Domanda

Code:

var buster = require('buster'),
    NumberCruncher = require('../src/NumberCruncher');

buster.testCase('Number Cruncher', {
    setUp: function() {
        this.numberCruncher = new NumberCruncher();
    },
    tearDown: function() {
        delete this.numberCruncher;
    },
    'constructor returns numberCruncher': function() {
        assert(this.numberCruncher instanceof NumberCruncher);
    },
    'object constructor correct': function() {
        assert.equals(this.numberCruncher.constructor, NumberCruncher);
    },
    'can add numbers': function() {
        buster.assert.equals(this.numberCruncher.add(5,3), 8, 'NumberCruncher cannot add');
    }
});

Background:

In setUp, we are creating an object and setting it as a property of this (the testcase). In tearDown we are deleting said property.

Question:

If you had to explain the thought or reason behind the practice of deleting object properties in the tearDown method, what would you say? Why is this a good thing? Does it have benefits? Does it only have benefits when scaled to really big objects?

My Thoughts:

My reasoning (which may be wrong), is that we are guaranteeing garbage collection after each test is run.

È stato utile?

Soluzione

Whether or not garbage collection is being guaranteed is irrelevant. More important is resetting the value to a known state (in this case, undefined) so that there is no chance of creating a false positive or false negative test case by having dirty data from the last one.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top