Karma 测试在服务上抛出 Provider not found 错误
Karma test throws a Provider not found error on a service
我正在尝试创建一个简单的服务并在 jasmine-karma 环境中对其进行测试。
我用过Angular1.5.5
出于某种原因,我一直收到错误消息
Chrome 46.0.2490 (Windows 7 0.0.0) reddit api service should do something FAILED
Error: [$injector:unpr] Unknown provider: redditServiceProvider <- redditService
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=redditServiceProvider%20%3C-%20redditService
at c:/client/www/vendor/angular/angular.js:68:12
at c:/client/www/vendor/angular/angular.js:4502:19
at Object.getService [as get] (c:/client/www/vendor/angular/angular.js:4655:39)
at c:/client/www/vendor/angular/angular.js:4507:45
at getService (c:/client/www/vendor/angular/angular.js:4655:39)
at injectionArgs (c:/client/www/vendor/angular/angular.js:4679:58)
at Object.invoke (c:/client/www/vendor/angular/angular.js:4701:18)
at Object.workFn (c:/client/node_modules/angular-mocks/angular-mocks.js:3078:20)
Error: Declaration Location
at window.inject.angular.mock.inject (c:/client/node_modules/angular-mocks/angular-mocks.js:3040:25)
at Suite.<anonymous> (c:/client/www/app/mine/myspec.spec.js:12:16)
at c:/client/www/app/mine/myspec.spec.js:7:3
at c:/client/www/app/mine/myspec.spec.js:52:3
TypeError: Cannot read property 'whenGET' of undefined
at Object.<anonymous> (c:/client/www/app/mine/myspec.spec.js:18:18)
Chrome 46.0.2490 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.049 secs)
Chrome 46.0.2490 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.048 secs / 0.049 secs)
29 06 2016 13:03:43.349:DEBUG [karma]: Run complete, exiting.
29 06 2016 13:03:43.349:DEBUG [launcher]: Disconnecting all browsers
29 06 2016 13:03:43.650:DEBUG [launcher]: Process Chrome exited with code 0
29 06 2016 13:03:43.652:DEBUG [temp-dir]: Cleaning temp dir C:\Users\mine\AppData\Local\Temp\karma-78283773
29 06 2016 13:03:44.892:DEBUG [launcher]: Finished all browsers
[13:03:44] 'karma' errored after 15 s
[13:03:44] Error: 1
我的服务文件是:
angular.module("reddit",[]).service("userService",
function($http) {
return {
getSubredditsSubmittedToBy: function(user) {
return $http.get("http://api.reddit.com/user/" + user + "/submitted.json").then(function(response) {
var posts, subreddits;
posts = response.data.data.children;
// transform data to be only subreddit strings
subreddits = posts.map(function(post) {
return post.data.subreddit;
});
// de-dupe
subreddits = subreddits.filter(function(element, position) {
return subreddits.indexOf(element) === position;
});
return subreddits;
});
}
};
});
我的测试是这样的:
(function(){
"use strict";
describe("reddit api service", function () {
var redditService, httpBackend;
beforeEach(module("reddit"));
beforeEach(inject(function (_redditService_, $httpBackend) {
redditService = _redditService_;
httpBackend = $httpBackend;
}));
it("should do something", function () {
httpBackend.whenGET("http://api.reddit.com/user/yoitsnate/submitted.json").respond({
data: {
children: [
{
data: {
subreddit: "golang"
}
},
{
data: {
subreddit: "javascript"
}
},
{
data: {
subreddit: "golang"
}
},
{
data: {
subreddit: "javascript"
}
}
]
}
});
redditService.getSubredditsSubmittedToBy("yoitsnate").then(function(subreddits) {
expect(subreddits).toEqual(["golang", "javascript"]);
});
httpBackend.flush();
});
});
})();
我已经按照本教程(http://nathanleclaire.com/blog/2014/04/12/unit-testing-services-in-angularjs-for-fun-and-for-profit/)创建了这个服务(而不是我复制的)
我假设它确实有效并且还有其他一些问题正在发生。我得到了一些其他人的工作代码,并且任何经过测试的服务都可以正常工作。
请让我知道我哪里错了。
谢谢!
问题似乎在于您如何 naming/injecting Reddit 服务。在您的服务文件中,您创建了一个名为 userService
的模块,但在测试中您正在注入并使用一个名为 redditService
的模块,据我所知该模块不存在。我认为它会起作用,如果你这样做:
beforeEach(inject(function (_userService_, $httpBackend) {
redditService = _userService_;
httpBackend = $httpBackend;
}));
我正在尝试创建一个简单的服务并在 jasmine-karma 环境中对其进行测试。
我用过Angular1.5.5
出于某种原因,我一直收到错误消息
Chrome 46.0.2490 (Windows 7 0.0.0) reddit api service should do something FAILED
Error: [$injector:unpr] Unknown provider: redditServiceProvider <- redditService
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=redditServiceProvider%20%3C-%20redditService
at c:/client/www/vendor/angular/angular.js:68:12
at c:/client/www/vendor/angular/angular.js:4502:19
at Object.getService [as get] (c:/client/www/vendor/angular/angular.js:4655:39)
at c:/client/www/vendor/angular/angular.js:4507:45
at getService (c:/client/www/vendor/angular/angular.js:4655:39)
at injectionArgs (c:/client/www/vendor/angular/angular.js:4679:58)
at Object.invoke (c:/client/www/vendor/angular/angular.js:4701:18)
at Object.workFn (c:/client/node_modules/angular-mocks/angular-mocks.js:3078:20)
Error: Declaration Location
at window.inject.angular.mock.inject (c:/client/node_modules/angular-mocks/angular-mocks.js:3040:25)
at Suite.<anonymous> (c:/client/www/app/mine/myspec.spec.js:12:16)
at c:/client/www/app/mine/myspec.spec.js:7:3
at c:/client/www/app/mine/myspec.spec.js:52:3
TypeError: Cannot read property 'whenGET' of undefined
at Object.<anonymous> (c:/client/www/app/mine/myspec.spec.js:18:18)
Chrome 46.0.2490 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.049 secs)
Chrome 46.0.2490 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.048 secs / 0.049 secs)
29 06 2016 13:03:43.349:DEBUG [karma]: Run complete, exiting.
29 06 2016 13:03:43.349:DEBUG [launcher]: Disconnecting all browsers
29 06 2016 13:03:43.650:DEBUG [launcher]: Process Chrome exited with code 0
29 06 2016 13:03:43.652:DEBUG [temp-dir]: Cleaning temp dir C:\Users\mine\AppData\Local\Temp\karma-78283773
29 06 2016 13:03:44.892:DEBUG [launcher]: Finished all browsers
[13:03:44] 'karma' errored after 15 s
[13:03:44] Error: 1
我的服务文件是:
angular.module("reddit",[]).service("userService",
function($http) {
return {
getSubredditsSubmittedToBy: function(user) {
return $http.get("http://api.reddit.com/user/" + user + "/submitted.json").then(function(response) {
var posts, subreddits;
posts = response.data.data.children;
// transform data to be only subreddit strings
subreddits = posts.map(function(post) {
return post.data.subreddit;
});
// de-dupe
subreddits = subreddits.filter(function(element, position) {
return subreddits.indexOf(element) === position;
});
return subreddits;
});
}
};
});
我的测试是这样的:
(function(){
"use strict";
describe("reddit api service", function () {
var redditService, httpBackend;
beforeEach(module("reddit"));
beforeEach(inject(function (_redditService_, $httpBackend) {
redditService = _redditService_;
httpBackend = $httpBackend;
}));
it("should do something", function () {
httpBackend.whenGET("http://api.reddit.com/user/yoitsnate/submitted.json").respond({
data: {
children: [
{
data: {
subreddit: "golang"
}
},
{
data: {
subreddit: "javascript"
}
},
{
data: {
subreddit: "golang"
}
},
{
data: {
subreddit: "javascript"
}
}
]
}
});
redditService.getSubredditsSubmittedToBy("yoitsnate").then(function(subreddits) {
expect(subreddits).toEqual(["golang", "javascript"]);
});
httpBackend.flush();
});
});
})();
我已经按照本教程(http://nathanleclaire.com/blog/2014/04/12/unit-testing-services-in-angularjs-for-fun-and-for-profit/)创建了这个服务(而不是我复制的) 我假设它确实有效并且还有其他一些问题正在发生。我得到了一些其他人的工作代码,并且任何经过测试的服务都可以正常工作。 请让我知道我哪里错了。 谢谢!
问题似乎在于您如何 naming/injecting Reddit 服务。在您的服务文件中,您创建了一个名为 userService
的模块,但在测试中您正在注入并使用一个名为 redditService
的模块,据我所知该模块不存在。我认为它会起作用,如果你这样做:
beforeEach(inject(function (_userService_, $httpBackend) {
redditService = _userService_;
httpBackend = $httpBackend;
}));