微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个控制器中有一个调用功能
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();
});

原文地址:https://www.jb51.cc/angularjs/142848.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐