加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为角度控制器测试注入模拟服务

发布时间:2020-12-17 08:23:26 所属栏目:安全 来源:网络整理
导读:我试图测试一个依赖于我自己建立的服务的控制器。我想从这个服务中嘲笑这个服务,因为这个服务与DOM进行了交谈。 这是我目前的测试: describe('Player Controllers',function () { beforeEach(function () { this.addMatchers({ toEqualData: function (exp
我试图测试一个依赖于我自己建立的服务的控制器。我想从这个服务中嘲笑这个服务,因为这个服务与DOM进行了交谈。

这是我目前的测试:

describe('Player Controllers',function () {

    beforeEach(function () {
        this.addMatchers({
            toEqualData: function (expected) {
                return angular.equals(this.actual,expected);
            }
        });
    });

    describe('TestPSPlayerModule',function () {
        var $httpBackend,scope,ctrl;

        beforeEach(module('PSPlayerModule'));

        beforeEach(inject(function (_$httpBackend_,$rootScope,$controller) {
            $httpBackend = _$httpBackend_;

            scope = $rootScope.$new();
            ctrl = $controller(PlayerController,{ $scope: scope });
        }));

        it('should request a clip url from the server when clipClicked is called',function () {
            expect(1).toBe(1);
        });
    });

});

我的控制器如下所示:

w.PlayerController = function ($scope,$http,$window,speedSlider,$location) {
    ...
}

所以这是speedSlider我想嘲笑。

我有一个想法是使用我在测试代码中创建的模块,可以提供速度滑块的假的实现,所以我将以下内容添加到test.js文件的顶部:

module('TestPSPlayerModule',[]).factory('speedSlider',function () {
    return = {
       ...
    };
});

然后在beforeEach()调用中列出该模块,而不是具体的,但是如果我这样做,我会收到以下错误:

Injector already created,can not register a module!

所以我认为我必须有一个更好的方法来提供我的一个服务的模拟实现。我可以使用sinon.js ….

确定当您使用模块定义后,您没有额外的括号。 所以模块(‘TestPSPlayer’)而不是模块(‘TestPSPlayer’,[])。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读