Angularjs mocha:在每次抛出异常之前使用InjectInside
我已经编写了一个测试控制器,它返回以下对象Angularjs mocha:在每次抛出异常之前使用InjectInside,angularjs,mocha.js,karma-runner,karma-mocha,Angularjs,Mocha.js,Karma Runner,Karma Mocha,我已经编写了一个测试控制器,它返回以下对象 {hello: 'a', world: 'b'}; 用摩卡咖啡来测试它 describe('Test suite', function(){ let controller; beforeEach(window.module('app')); beforeEach(function(){ inject(function($controller){ controller =
{hello: 'a', world: 'b'};
用摩卡咖啡来测试它
describe('Test suite', function(){
let controller;
beforeEach(window.module('app'));
beforeEach(function(){
inject(function($controller){
controller = $controller('simpleController');
console.info(controller);
});
});
afterEach(function(){
console.info("afterEach called");
});
describe('Controller', function(){
it('it should have hello', function(){
controller.should.have.property('hello')
});
it('it should have world', function(){
controller.should.have.property('world');
});
});
});
然而,当在beforeach中第二次调用inject时,第二个测试用例异常失败
✖ "before each" hook for "it should have world"
Chrome 57.0.2987 (Mac OS X 10.12.1)
Error: the string "textAngular Error: A unique name is required for a Tool Definition" was thrown, throw an Error :)
如果我没有第二次打电话给inject,一切都很好
if (!controller) {
inject(function($controller){
controller = $controller('simpleController');
console.info(controller);
});
}
理想情况下,我希望在每个测试用例之前创建一个新的controller实例。当然,应该创建一个新的controller实例,这是很自然的。发布的代码中没有会导致错误的内容
textAngular错误
显然来自textAngular。这可能不适合重新引导。只是不要使用app
module,因为你已经在测试中获得了所有第三方的东西。因为它们可以(而且确实)产生副作用。@estus解决了这个问题。