Angular:注入服务时单元测试失败

时间:2015-08-20 19:35:25

标签: angularjs unit-testing ionic karma-jasmine

我开始对现有的Angular/ionic应用安装karmajasmine进行单元测试。 我为服务编写了第一个单元测试,但它失败并得到了error

Error: [$injector:unpr] Unknown provider: followServiceProvider <- followService

我想知道我的注射方式是否正确! 为了确保我的设置正常,我开始安装新的ionic应用karma并且jasmine尝试了简单的测试并且运行了

以下是服务followService的代码:

'use strict';

 angular.module('myApp.services')
    .factory('followService', function($http, $q, API_URL) {

        var url = API_URL + 'api/users/followers' ;

        function getFollowers() {
            var deferred = $q.defer();
            $http.put(url)
                .success(function(data, status, headers, config) {
                    deferred.resolve(data);
                })
                .error(function(data, status, headers, config) {
                    deferred.reject(data);

                });
            return deferred.promise;
        }

    });

这是它的单元测试:

'use strict';

describe('Service: followService Test Follow APIs', function () {
    // load the service's module
    beforeEach(module('myApp.services'));

    // instantiate service
    var followService;
    var $httpBackend;

    beforeEach(inject(function (_$httpBackend_, _followService_) {
        followService = _followService_;
        $httpBackend = _$httpBackend_;
    }));

      it('should GET Followers from the server', function () {
        $httpBackend.expectGET('api/users/followers').respond(200, {"id": "55d1c1acddb6dabe468bbba1", "displayName": "Brand1", "pictrue": "path/to/picture"});

        var followers = followService.getFollowers();

        $httpBackend.flush();

        expect(followers).not.toBe(null);
    });
});

0 个答案:

没有答案
相关问题