How to Jasmine test code within angular module run block

前端 未结 1 923
小鲜肉
小鲜肉 2021-01-04 08:19

I would like to Jasmine test that Welcome.go has been called. Welcome is an angular service.

angular.module(\'welcome\',[])
  .run(function(Welcome) {
    We         


        
1条回答
  •  借酒劲吻你
    2021-01-04 08:37

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

    0 讨论(0)
提交回复
热议问题