Angular注入$ service导致未知提供者:$ serviceProvider

时间:2014-01-17 12:45:11

标签: angularjs jasmine

为什么当我在业力跑步者中运行时:

describe('Service tests', function () {


    var DataServiceMock
    var httpBackend;


    beforeEach(angular.mock.module('app'));

 beforeEach(angular.mock.inject(function( $httpBackend, $service, DataService,  $injector){

导致此错误

  

错误:[$ injector:unpr]未知提供者:$ serviceProvider< - $ service     http://errors.angularjs.org/1.2.1/ $注射器/ unpr?P0 =%24serviceProvider%20%3 C-%20%24service         在/home/site/angular/angular.js:78:12

修改2_ _ __ _ __ _ __ _ __ _ __ _ __ _

我正在尝试为dataHandlerService模拟DataService

describe('Service', function () {


    var DataServiceMock
    var httpBackend;

    var testUrl = "test/";

    beforeEach(angular.mock.module('app'));

  beforeEach(angular.mock.inject(function(){
    module(function ($provide) {
            $provide.value('DataService', DataServiceMock)
        })
    }));

    it('should have', inject(function(DataService) {
        expect('DataService').not.toBe(null);
    }));

并出现此错误:

  

错误:注射器已经创建,无法注册模块!         at workFn(/home/me/root/angular/angular-mocks.js:1985:15)

1 个答案:

答案 0 :(得分:3)

没有$服务。如果您想模拟您的服务,那么您应该使用$ provide:

来实现
beforeEach(function () {
  DataServiceMock= {}
  DataServiceMock.doSomething = function() {}
  module(function ($provide) {
    $provide.value('DataService', DataServiceMock)
  })
})