AngularJS 用 Mocha 测试工厂
AngularJS Test a Factory with Mocha
我是 Mocha 和 AngularJS 单元测试的新手,但想使用 Mocha 测试我的应用程序。我有基本的语言测试工作,但我无法 运行 针对我的应用程序工厂或控制器进行测试。
我有以下基本文件。
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)
你收到一个错误,因为 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.
我是 Mocha 和 AngularJS 单元测试的新手,但想使用 Mocha 测试我的应用程序。我有基本的语言测试工作,但我无法 运行 针对我的应用程序工厂或控制器进行测试。
我有以下基本文件。
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)
你收到一个错误,因为 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.