2016-06-10 155 views
0

我遇到了一些麻烦,单元测试了一个简单的用户服务,它暴露了两个函数。 测试和服务如下。 每当我运行测试,我不断收到以下错误..AngularJS单元测试服务

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); 
    }])); 



}); 

如果一个测试运行,并且我能够为了正确地获得用户服务实例,我可以继续编写其余的测试,但由于某种原因,它不起作用。有人可以帮我对此并阐明我做错了什么在这里一些轻..

回答

0

should be

module('y2yApp'); 

这是CommonJS的不安全的快捷方式

angular.mock.module('y2yApp'); 
+0

我这样做,但没有运气与测试传递.. – Sharath

+0

这是您已发布的代码中的错误。它未通过的原因可能是Karma配置或任何其他在幕后的配置。 – estus

+0

是的,我的应用程序初始化存在错误 – Sharath