2015-08-25 101 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.