当它依赖于其他模块时,如何单独测试服务?

时间:2013-10-29 14:59:08

标签: angularjs jasmine

如何测试$myService

$myService取决于其他服务$foo。服务$foo位于不同的模块中。

// a service.
angular.module("plunker").factory('$foo', function () {
    return {
        bar: function (msg) {
            //do something here.
        }
    };
});

// a service that depends on the other service.
angular.module("app").factory('$myService', function ($foo) {
    return {
        test: function (msg) {
            $foo.bar(msg);
        }
    };
});

这个测试给了我一个错误:

describe('testing some service', function (){
  var svc,
      mockFooSvc;

  beforeEach(function (){
    module('app', function($provide) {

      mockFooSvc = {
        bar: function (){}
      };

      spyOn(mockFooSvc, 'bar');

      $provide.value('$foo', mockFooSvc);
    });

    //now we inject the service we're testing.
    inject(function($myService) {
      svc = $myService;
    });
  });

  it('should call $foo.bar on $myService.test passing through parameters.', function (){
    //make the call.
    svc.test('WEE!');

    //check our spy to see if bar was called properly.
    expect(mockFooSvc.bar).toHaveBeenCalledWith('WEE!');
  });
});

错误:

x Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module plunker due to:
Error: [$injector:nomod] Module 'plunker' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.0-rc.2/$injector/nomod?p0=plunker

在我的app.JS中,我已经添加了plunker作为依赖项。

var app = angular.module("app", ["ngCookies", "ngResource", "ngRoute", "tyNav", "http-auth-interceptor", "plunker"]);

0 个答案:

没有答案
相关问题