0

如何测试我的$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"]);
4

0 回答 0