我正在尝试创建一个简单的服务并在 jasmine-karma 环境中对其进行测试。
我用过 Angular 1.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/)创建此服务(而不是我复制它)我认为它确实有效,并且还有其他一些问题正在发生。我得到了其他人的工作代码,任何经过测试的服务都可以正常工作。请让我知道我错在哪里。谢谢!