【问题标题】:How to invoke spyOn on a scope function如何在作用域函数上调用 spyOn
【发布时间】:2015-06-02 15:38:11
【问题描述】:

我有以下茉莉花规格。

describe('ViewMeetingCtrl', function () {
    var $rootScope, scope, $controller , $q  ;

   beforeEach(angular.mock.module('MyApp'));

   beforeEach(inject(function ($rootScope, $controller ) {
        scope = $rootScope.$new();
        createController = function() {
            return $controller('ViewMeetingCtrl', {
            $scope: scope,
            meeting : {}
            }); 
        };
    }));

    it('the meeting type should be equal to an object', function () {
        var controller = new createController();
        //some assertion
    });

});

以下是我的 ViewMeetingCtrl.js

(function () {
    'use strict';
    angular.module('MyApp').controller('ViewMeetingCtrl', ViewMeetingCtrl);

    ViewMeetingCtrl.$inject = ['$scope', '$state', '$http', '$translate', 'notificationService', 'meetingService', '$modal', 'meeting', 'attachmentService'];

        function ViewMeetingCtrl($scope, $state, $http, $translate, notificationService, meetingService, $modal, meeting, attachmentService) {
            $scope.meeting = meeting;

            $scope.cancelMeeting = cancelMeeting;

            function cancelMeeting(meetingId, companyId) {
                meetingService.sendCancelNotices(companyId, meetingId)
                    .success(function () {
                        $state.go('company.view');
                    });
            }

            //more code
        }
    })();

我的问题是如何在上述 cancelMeeting() 上调用 spyOn(或任何其他茉莉间谍相关方法)方法,以便我可以模拟方法调用、返回等。我做了关注

describe('ViewMeetingCtrl', function () {
    var $rootScope, scope, $controller , $q  ;

   beforeEach(angular.mock.module('MyApp'));

   beforeEach(inject(function ($rootScope, $controller ) {
        scope = $rootScope.$new();
        createController = function() {
            return $controller('ViewMeetingCtrl', {
            $scope: scope,
            meeting : {}
            }); 
        };
    }));

    it('the meeting type should be equal to an object', function () {
        spyOn(scope, 'cancelMeeting');//cancelMeeting is inside the scope so did like this
        var controller = new createController();
    });

});

但我得到以下输出

Firefox 37.0.0 (Windows 8.1) ViewMeetingCtrl the meeting type should be equal to an object FAILED
        Error: cancelMeeting() method does not exist in C:/Users/Work/MyApp/Tests/node_mo
dules/jasmine-core/lib/jasmine-core/jasmine.js (line 1895)

我调用 spyOn 的方式是错误的还是我缺少任何其他语法?还是我在这里遗漏了一些基本的东西?

【问题讨论】:

    标签: angularjs unit-testing jasmine karma-runner karma-jasmine


    【解决方案1】:

    在创建控制器之前,cancelMeeting 函数不会添加到作用域中。所以我认为你只需要反转测试代码中的行:

    it('the meeting type should be equal to an object', function () {
        var controller = new createController();
        spyOn(scope, 'cancelMeeting');
    });
    

    【讨论】:

    • 谢谢。你是对的,在创建控制器之前调用 spyOn 是问题:)
    【解决方案2】:

    您的测试代码看起来不错。我认为您只需要切换作业的顺序即可。首先定义 cancelMeeting 然后分配它。

    function cancelMeeting(meetingId, companyId) {
        meetingService.sendCancelNotices(companyId, meetingId)
            .success(function () {
                $state.go('company.view');
            });
    }
    
    $scope.cancelMeeting = cancelMeeting;
    

    或者只是:

    $scope.cancelMeeting = function(meetingId, companyId) {
        meetingService.sendCancelNotices(companyId, meetingId)
           .success(function () {
                $state.go('company.view');
            });
    }
    

    【讨论】:

    • 感谢您的回复。但问题是在创建控制器之前调用 spyOn,请参阅 John Case 的回复
    • 好点。但是为什么不直接在'beforeEach'中创建控制器呢?然后可以立即调用 spyOn 方法。和示例(不管 cmets 中的格式是否错误): var scope, controller; beforeEach(inject(function ($rootScope, $controller) { scope = $rootScope.$new(); controller = $controller('yourController', { $scope: scope }); })); it('将测试一些东西', inject(function () { spyOn(scope, "getMethod").and.callFake(function () { return "something"; });
    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2020-04-08
    • 2021-11-03
    相关资源
    最近更新 更多