Javascript 为什么要在拆卸(单元测试)中删除[object].[property]
代码:Javascript 为什么要在拆卸(单元测试)中删除[object].[property],javascript,unit-testing,buster.js,Javascript,Unit Testing,Buster.js,代码: var buster = require('buster'), NumberCruncher = require('../src/NumberCruncher'); buster.testCase('Number Cruncher', { setUp: function() { this.numberCruncher = new NumberCruncher(); }, tearDown: function() { del
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');
}
});
背景:
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');
}
});
在setUp
中,我们正在创建一个对象,并将其设置为this
(testcase)的属性。在拆卸中
我们正在删除所述财产
问题:
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');
}
});
如果您必须解释在tearDown
方法中删除对象属性的做法背后的想法或原因,您会怎么说?为什么这是件好事?它有好处吗?它只在缩放到真正大的对象时才有好处吗
我的想法:
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');
}
});
我的推理(可能是错误的)是,我们保证在每个测试运行后进行垃圾收集。是否保证垃圾收集是无关紧要的。更重要的是将该值重置为已知状态(在本例中为未定义状态),这样就不可能通过上一个测试中的脏数据创建假阳性或假阴性测试用例。但是如果在每个测试开始时调用安装程序,并且该值被覆盖,难道创建假阳性的机会没有被消除吗?在这种情况下是的,但是如果您在测试用例中初始化值(或不初始化),那么就更容易出错。老实说,我不知道你为什么要知道。