我写了一个AngularJS服务,我想对它进行单元测试.
angular.module('myServiceProvider', ['fooServiceProvider', 'barServiceProvider']).
factory('myService', function ($http, fooService, barService) {
this.something = function() {
// Do something with the injected services
};
return this;
});
Run Code Online (Sandbox Code Playgroud)
我的app.js文件已注册:
angular
.module('myApp', ['fooServiceProvider','barServiceProvider','myServiceProvider']
)
Run Code Online (Sandbox Code Playgroud)
我可以测试DI是这样工作的:
describe("Using the DI framework", function() {
beforeEach(module('fooServiceProvider'));
beforeEach(module('barServiceProvider'));
beforeEach(module('myServiceProvder'));
var service;
beforeEach(inject(function(fooService, barService, myService) {
service=myService;
}));
it("can be instantiated", function() {
expect(service).not.toBeNull();
});
});
Run Code Online (Sandbox Code Playgroud)
这证明了服务可以由DI框架创建,但是接下来我想对服务进行单元测试,这意味着模拟注入的对象.
我该怎么做呢?
我已经尝试将模拟对象放在模块中,例如
beforeEach(module(mockNavigationService));
Run Code Online (Sandbox Code Playgroud)
并将服务定义重写为:
function MyService(http, fooService, barService) {
this.somthing = function() {
// Do something with the injected services
};
});
angular.module('myServiceProvider', …Run Code Online (Sandbox Code Playgroud) 我正在为业力编写一个单元测试,无法让它运行.它似乎打破了注入功能.我认为这与我如何在测试中获得控制器有关,但无法找到解决方案.
我刚刚开始使用角度在过去的几天,所以任何建议将不胜感激,谢谢!
错误:
Error: Argument 'fn' is not a function, got string
at Error (<anonymous>)
at $a (path/app/lib/angular.js:16:453)
at qa (path/app/lib/angular.js:17:56)
at Cb (path/app/lib/angular.js:24:458)
at Object.d [as invoke] (path/app/lib/angular.js:27:66)
at path/app/lib/angular.js:26:194
at Array.forEach (native)
at m (path/app/lib/angular.js:6:192)
at e (path/app/lib/angular.js:25:298)
at Object.sb [as injector] (path/app/lib/angular.js:29:360)
TypeError: Cannot read property 'zip' of undefined
at null.<anonymous> (path/test/unit/controllersSpec.js:26:19)
Run Code Online (Sandbox Code Playgroud)
测试:
'use strict';
/* jasmine specs for controllers go here */
describe('influences controllers', function() {
beforeEach(module('influences.controllers', ['ui.bootstrap', 'influences.services']));
describe('IndividualCtrl', function(){
var scope, ctrl, service, $httpBackend; …Run Code Online (Sandbox Code Playgroud)