Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs mocha:在每次抛出异常之前使用InjectInside_Angularjs_Mocha.js_Karma Runner_Karma Mocha - Fatal编程技术网

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解决了这个问题。