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

单元测试 – 在茉莉花测试中模拟角度

发布时间:2020-12-17 07:42:18 所属栏目:安全 来源:网络整理
导读:我有一个控制器中有一个调用的功能 var someVar = angular.element(event.target).scope().field; 我试图通过做嘲笑它 var ngElementFake = function(el) { return { scope: function() { return { toggleChildElement: true,field: scope.field } } } }spyO
我有一个控制器中有一个调用的功能
var someVar = angular.element(event.target).scope().field;

我试图通过做嘲笑它

var ngElementFake = function(el) {
                return {
                    scope: function() {
                        return {
                            toggleChildElement: true,field: scope.field
                        }
                    }
                }
            }

spyOn(angular,'element').andCallFake(ngElementFake);

但是当我在测试中调用该函数时,我得到了响应:

TypeError: 'undefined' is not a function (evaluating 'injector.get('$rootElement').off()')
at ../angular-mocks/angular-mocks.js:1819

我究竟做错了什么?

编辑:注射

beforeEach(function() {
        inject(function($rootScope,$controller) {

            scope = $rootScope;

            scope.record = recordData;

            scope.model = 'Hierarchy';

            ctrl = $controller("fngHierarchyChildCtrl",{
                $scope: scope
            });
        });
    });
我能够通过在回调后手动清除间谍来解决这个问题.
var spy;

beforeEach(function() {
    spy = spyOn(angular,'element').andCallFake(ngElementFake);
});

afterEach(function() {
    spy.andCallThrough();
});

(编辑:李大同)

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

    推荐文章
      热点阅读