简体   繁体   English

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

[英]How to Jasmine test code within angular module run block

I would like to Jasmine test that Welcome.go has been called. 我想Jasmine测试Welcome.go已被调用。 Welcome is an angular service. 欢迎是一个有角度的服务。

angular.module('welcome',[])
  .run(function(Welcome) {
    Welcome.go();
  });

This is my test so far: 这是我到目前为止的测试:

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

Note: 注意:

  • welcome (lowercase w) is the module 欢迎(小写w)是模块
  • Welcome (uppercase W) is the service 欢迎(大写W)是服务

Managed to figure it out. 管理好解决它。 Here is what I came up with: 这是我想出的:

'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();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM