问题描述
我是用茉莉花来测试AngularJS工厂。
I am using Jasmine to test an AngularJS factory.
我有困难测试工厂具有依赖性。我已经包括了code为工厂我测试,测试code。
I am having difficulty testing a factory that has a dependency. I have included the code for the factory I am testing and the test code.
问题是,我得到的错误和测试失败。
The problem is that I am getting errors and the test is failing.
这是我看到的错误:
Chrome 33.0.1750 (Mac OS X 10.8.5) Testing my Test Service can get an instance of my factory FAILED
Error: [$injector:unpr] http://errors.angularjs.org/1.2.8/$injector/unpr?p0=TestThisServiceProvider%20%3C-%20TestThisService
at Error (native)
at /Users/.../www/lib/angular/angular.min.js:6:449
at /Users/.../www/lib/angular/angular.min.js:32:125
at Object.c [as get] (/Users/.../www/lib/angular/angular.min.js:30:200)
at /Users/.../www/lib/angular/angular.min.js:32:193
at c (/Users/.../www/lib/angular/angular.min.js:30:200)
at Object.d [as invoke] (/Users/.../www/lib/angular/angular.min.js:30:417)
at workFn (/Users/.../www/lib/angular/angular-mocks.min.js:6:20731)
Error: Declaration Location
at window.inject.angular.mock.inject (/Users/.../www/lib/angular/angular-mocks.min.js:6:20394)
at null.<anonymous> (/Users/.../www/tests/myapp/TestServices/controllers.tests.js:54:43)
at /Users/.../www/tests/myapp/TestServices/controllers.tests.js:1:1
Firefox 27.0.0 (Mac OS X 10.8) Lighting Control Service can get an instance of my factory FAILED
Error: [$injector:unpr] http://errors.angularjs.org/1.2.8/$injector/unpr?p0=TestThisServiceProvider%20%3C-%20TestThisService
F/<@/Users/.../www/lib/angular/angular.min.js:6
$b/l.$injector<@/Users/.../www/lib/angular/angular.min.js:32
c@/Users/.../www/lib/angular/angular.min.js:30
$b/p.$injector<@/Users/.../www/lib/angular/angular.min.js:32
c@/Users/.../www/lib/angular/angular.min.js:30
d@/Users/.../www/lib/angular/angular.min.js:30
workFn@/Users/.../www/lib/angular/angular-mocks.min.js:6
angular.mock.inject@/Users/.../www/lib/angular/angular-mocks.min.js:6
@/Users/.../www/tests/myapp/TestServices/controllers.tests.js:54
@/Users/.../www/tests/myapp/TestServices/controllers.tests.js:1
工厂测试
angular.module('myApp.Module', ["ngResource"])
.factory('TestThisService', function(WebSocketSrvc, $q, $rootScope, $log) {
return {
createData: function(type, msg_object, cb_URI, cbfunction) {
// creates
},
requestData: function(type, cb_URI) {
// requests
},
updateData: function(type, id, msg_object, cb_URI, cbfunction) {
// updates
},
deleteData: function(type, id, cb_URI, cbfunction) {
// deletes
}
}
});
测试code:
describe("My Service", function() {
// Load your module.
beforeEach(module('myApp.Module','myApp.ModuleTwo'));
// Setup the mock service in an anonymous module.
beforeEach(module(function ($provide) {
$provide.value('WebSocketSrvc', {
variable: 'value',
get: function() {
return 'test';
},
send: function(request, cb_URI) {
return 'result';
}
// ... more functions included not displayed.
});
}));
it('can get an instance of my factory', inject(function(TestThisService) {
expect(TestThisService).toBeDefined();
}));
});
我的问题是什么,我需要做的就是我的单元测试顺利过关?
My question is what do I need to do to get my unit test to pass successfully?
推荐答案
在code你给你有2个错别字,但我想这是不是你真正的code:
In the code you gave you have 2 typos, but I guess this isn't your real code:
-
测试code,4号线:
Test Code, line 4:
beforeEach(module('MyApp.Module'));
应该是:
beforeEach(module('myApp.Module'));
和第13行,你忘了在报价:回归测试;
当你的错误是TestThisServiceProvider ...,我会用 $注射器
在 beforeEach
注入你的服务进行测试。只是之后的第二 beforeEach(模块(函数(){...});
As the error you get is "TestThisServiceProvider...", I would use $injector
in a beforeEach
to inject your service to test. Just after the second beforeEach(module(function(){...});
beforeEach(inject(function($injector) {
myService = $injector.get('TestThisService');
}));
我觉得照顾的依赖关系(如 ngResource
你的情况)。
这篇关于失败的工厂依赖的单元测试使用茉莉花&放AngularJS;因果报应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!