14

呼び出しを持つコントローラーに関数があります

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

3 に答える 3

18

アフターコールバックでスパイを手動でクリアすることで、これを修正できました。

var spy;

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

afterEach(function() {
    spy.andCallThrough();
});
于 2015-07-07T00:36:54.057 に答える
1

AngularJS FAQから:

bind()/unbind() ではなく on()/off() を使用するように変更されたため、Angular 1.2 は jQuery 1.7.1 以降でのみ動作します。

そのため、jquery 1.7.1 以降にアップグレードするか、jquery をまったく使用しないでください。angular は独自の jQLite を使用します。

于 2013-11-21T01:05:26.423 に答える