如何在角度模块运行块中的Jasmine测试代码

And*_*rew 12 javascript unit-testing jasmine angularjs angular-mock

我想Jasmine测试Welcome.go已被调用.欢迎是一个有角度的服务.

angular.module('welcome',[])
  .run(function(Welcome) {
    Welcome.go();
  });
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止的测试:

describe('module: welcome', function () {

  beforeEach(module('welcome'));

  var Welcome;
  beforeEach(inject(function(_Welcome_) {
    Welcome = _Welcome_;
    spyOn(Welcome, 'go');
  }));

  it('should call Welcome.go', function() {
    expect(Welcome.go).toHaveBeenCalled();
  });
});
Run Code Online (Sandbox Code Playgroud)

注意:

  • 欢迎(小写w)是模块
  • 欢迎(大写W)是服务

And*_*rew 21

管理好解决它.这是我想出的:

'use strict';

describe('module: welcome', function () {

  var Welcome;

  beforeEach(function() {
    module('welcome', function($provide) {
      $provide.value('Welcome', {
        go: jasmine.createSpy('go')
      });
    });

    inject(function (_Welcome_) {
      Welcome = _Welcome_;
    })
  });


  it('should call Welcome.go on module run', function() {
    expect(Welcome.go).toHaveBeenCalled();
  });
});
Run Code Online (Sandbox Code Playgroud)