我使用grunt-contrib-jasmine
来测试我的AMD模块。开箱即用似乎测试正在影响彼此。
输出不是按文件分开测试。这可以通过在测试beforeEach
中记录某些内容来验证。在所有测试中,对所有测试文件执行相同的回调。
如何将测试与彼此隔离,即由测试规范文件分隔?是否是添加另一级嵌套的唯一解决方案?
grunt config
options: {
specs: 'test/specs/unit/**/*spec.js',
keepRunner: true,
template: require('grunt-template-jasmine-requirejs'),
templateOptions: {
requireConfig: requireConfig
}
}
sample1.spec.js:
define(['Squire', 'sinon'], function(Squire, sinon){
'use strict';
var sut,
injector,
fakeServer;
beforeEach(function(done){
fakeServer = sinon.fakeServer.create();
console.log('create fake server'); // this is logged for all test files
injector = new Squire();
injector.require(['core/http-service'], function(httpService) {
sut = httpService;
done();
});
});
afterEach(function(){
fakeServer.restore();
injector.remove();
});
it('', function(){
expect(1).toBe(1);
});
});
答案 0 :(得分:3)
问题是您在beforeEach
之外有afterEach
和describe
。这意味着它们将在grunt-contrib-jasmine
找到的每个测试之前和之后被调用。
如果您希望它们仅用于it
- ed模块中的define
,则需要将它们放在describe
内:
define(['Squire', 'sinon'], function(Squire, sinon){
'use strict';
describe('some description', function(){
var sut,
injector,
fakeServer;
beforeEach(function(done){
fakeServer = sinon.fakeServer.create();
console.log('create fake server'); // this is logged for all test files
injector = new Squire();
injector.require(['core/http-service'], function(httpService) {
sut = httpService;
done();
});
});
afterEach(function(){
fakeServer.restore();
injector.remove();
});
it('', function(){
expect(1).toBe(1);
});
});
});