2015-08-25 77 views
0

我是摩卡和AngularJS单元测试的新手,但想使用摩卡来测试我的应用程序。我有基本的语言测试工作,但我无法对我的应用程序Factory或Controller运行测试。AngularJS使用摩卡测试工厂

我有以下基本文件。

apps.js

aangular.module('MyApp', []); 

file1.js

angular.module('MyApp').factory('Factory1' ...); 

file2.js

angular.module('MyApp').factory('Factory2' ...); 
angular.module('MyApp').factory('Controller' ...); 

describe('Main Test', function() { 
    var FactoryToTest; 
    beforeEach(module('MyApp')); 

    beforeEach(inject(function (_Factory_) { 
     FactoryToTest = _Factory_; 
    })); 

    describe('Factory2', function() { 
     it('should return "unknown"', function() { 
      Game = {}; 
      expect(new Factory2(Game)).to.equal('unknown'); 
     }); 
    }); 
}); 

当我运行测试,它会产生一个错误,我不知道要解决这个问题需要解决的问题。

错误:

Message: 
    object is not a function 
Stack: 
TypeError: object is not a function 
    at Suite.<anonymous> (b:\app\test.js:5:16) 

回答

1

你得到一个错误,因为beforeEach功能应采取一个回调函数,而不是一个对象。根据Angular guide on module unit testing(滚动到页面底部):

Each module can only be loaded once per injector. Usually an Angular app has only one injector and modules are only loaded once. Each test has its own injector and modules are loaded multiple times.