的$装载模拟在模态试验茉莉花 [英] Loading mock of $modal in Jasmine tests

查看:154
本文介绍了的$装载模拟在模态试验茉莉花的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

假设我有模块

angular.module('myModule',['$strap'] );//module depends on AngularStrap

Mymodule中已定义的服务如下:

myModule has service defined as following :

angular.module('myModule').service('myModuleService', ['$modal', function ($modal) 
{ ....
}); //depends on $modal service from AngularStrap

现在我需要测试myModuleService,我需要模拟模式$服务
在试验(茉莉为主)myModuleService加载如下

Now I need to test myModuleService and I need to mock $modal service In test ( jasmine based ) myModuleService loaded as following

var modalMock = jasmine.createSpy();
..
angular.mock.module('myModule');
...
$provide.value('$modal', modalMock);
..
angular.mock.inject(function ($injector) {
// Initialize the service under test instance
   myService = $injector.get('myModuleService');      
});

问题是,当myModuleService被加载时,也是真实的$模式的业务加载和NOT嘲弄$模态。结果
现在的问题是:什么是缺少在这里,以便与mockModal加载为myService,而不是与实际$模式?
在此先感谢

The problem is when myModuleService is loaded , also real $modal service is loaded and NOT mock of the $modal.
The question is: what is missing here in order to load myService with mockModal and not with real $modal ? Thanks in advance

推荐答案

我失去了你的一些测试实施细节,但以下工作:

I'm missing some of your test implementation details, but the following works:

describe('mock modal', function () {
    var modalMock = jasmine.createSpyObj('modal', ['show']),
        service;

    beforeEach(function () {
        angular.mock.module('myModule');
        angular.mock.module(function ($provide) {
            $provide.value('$modal', modalMock);
        });

        angular.mock.inject(function (myModuleService) {
            // Initialize the service under test instance
            service = myModuleService;
        });
    });

    it('mocks modal', function () {
        service.foo();
        expect(modalMock.show).toHaveBeenCalled();
    });
});

您可以找到完整的小提琴这里

这篇关于的$装载模拟在模态试验茉莉花的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆