シンプルなサービスを作成し、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/ ) に従って、このサービスを作成しました (むしろ私はコピーしました)それは機能し、他の問題が発生していると思います。私はいくつかの他の作業コードを取得しましたが、テストされたサービスはすべて正常に動作します。どこが間違っているか教えてください。ありがとう!