AngularJS单元测试服务

时间:2016-06-10 12:12:46

标签: angularjs unit-testing jasmine karma-runner

我遇到了一些麻烦,单元测试一个简单的用户服务,它暴露了两个功能。 测试和服务如下。 每当我运行测试时,我都会收到以下错误..

PhantomJS 2.1.1 (Mac OS X 0.0.0) User Service should contain a UserService FAILED
    Error: [$injector:unpr] Unknown provider: UserProvider <- User
    http://errors.angularjs.org/1.5.6/$injector/unpr?p0=UserProvider%20%3C-%20User (line 4501)

服务

angular.module('y2yApp').service('User', function($http) {

    this.profile = function() {
        return $http({
            method: 'GET',
            url: config.MW_URL + '/profile'
        }).then(function(response) {
            return {
                err: null,
                data: response.data
            };
        }, function(response) {
            return {
                err: response.data,
                data: null
            };
        });
    };

    this.update = function(userInfo) {
        return $http({
            method: 'POST',
            url: config.MW_URL + '/profile',
            data : userInfo
        });
    };
});

测试

describe('User Service', function() {
    beforeEach(function() {
        angular.module('y2yApp');

    });
    it('should contain a UserService',inject(['User',function(User) {
        console.log(User);
            expect(User).not.to.equal(null);
    }]));



});

如果这个测试运行,并且我能够正确获取用户服务实例,我可以继续编写其余的测试,但由于某种原因它无法正常工作。有人可以帮我解决这个问题,并对我在这里犯错的事情有所了解......

1 个答案:

答案 0 :(得分:0)

should be

module('y2yApp');

的CommonJS不安全快捷方式
angular.mock.module('y2yApp');
相关问题