【发布时间】:2019-03-18 14:23:30
【问题描述】:
所以我的测试中有这两个案例。第一个工作正常,在第二个中,我尝试在外面提取 beforeEach 声明,但它失败了,但我不明白为什么。这是一个简单的案例,基本上我尝试定义一个数组并在其上创建一个循环,以便使用不同的 beforeEach 参数声明多次运行测试。
案例 1
var params;
describe('When initializing', function () {
beforeEach(function () {
params = {
name: 'test 1'
};
});
it('should ..', function () {
params.name = 'test 2';
expect(...); => success
});
it('should ..', function () {
expect(...); => success because it expects params.name to be 'test 1' and it is 'test 1'
});
});
案例 2
var params;
var test = {
name: 'test 1'
};
describe('When initializing', function () {
beforeEach(function () {
params = test;
});
it('should ..', function () {
params.name = 'test 2';
expect(...); => success
});
it('should ..', function () {
expect(...); => fails because it expects params.name to be 'test 1' and it is 'test 2'
});
});
在第二个测试中,如果我在描述中使用console.log(test.name),我将得到test 2,即使之前的it 只是params.name = 'test 2'; 而不是test.name = 'test 2';,它也会以某种方式被覆盖
【问题讨论】:
标签: javascript unit-testing jasmine karma-runner