Jasmine:期望的间谍<function>被称为错误

时间:2017-10-27 10:18:29

标签: jasmine

在我的角度应用程序中,我正在尝试为下面的场景编写一个测试用例,但是得到错误'预期的spy reinvite已被调用。'。我正在测试“jasmine-core:^ 2.5.2和”业力:^ 1.3.0“。我写了类似的测试用例,他们没有错误地通过。

在我的控制器文件中:

function reinvite() {

        var emailsToReInvite = $j.map($scope.settingsData.userSettingsDetails, function(user) {
             if(user.reInviteChecked){
                return user.email;  
            }
        });

        if (emailsToReInvite.length >= 1) {

            var invitation = { invitees: emailsToReInvite, listId: listId};
            invitation = JSON.stringify(invitation);

            inviteCollaboratorsModalDataService.reinvite(invitation).then(
                function success(response) {
                    if(response.data.messages[0].code == 214){
                        $scope.showReinviteSuccess = true;

                        }
                    }else{
                        $scope.showReinviteSuccess = false;
                    }
                }
            );
        }
    }

我的spec文件:

describe('settingsModalController', function() {
var controllerUnderTest = "settingsModalController";    
var controllerResolver, rootScope, injector, $scope, inviteCollaboratorsModalDataService

beforeEach(function(){
    angular.mock.module('sharedListApp');

    inject(function($controller, $rootScope, $injector){
        controllerResolver = $controller;
        rootScope = $rootScope;
        injector = $injector;
    });

    $scope = rootScope.$new();
    inviteCollaboratorsModalDataService = injector.get('uiCommon.inviteCollaboratorsModalDataService');
});

it("should send re-invitation mail to selected users successfully", shouldReinviteSelectedUsers);

function shouldReinviteSelectedUsers() {
    var $q = injector.get('$q');
    $scope.settingsData = {
        userSettingsDetails: [{email: 'abc@xyz.com'}]
    };
    var data = { 
        messages: [ { code: 214 }],
        invite: {
            invitation: $scope.settingsData
        }
    };
    var response = { data: data };
    var mockResult = new $q.defer();
    mockResult.resolve(response);

    spyOn(inviteCollaboratorsModalDataService, 'reinvite').and.returnValue(mockResult.promise);

    controllerResolver(controllerUnderTest, { $scope: $scope });

    $scope.reinvite();
    $scope.showReinviteSuccess = true;
    $scope.$apply();

    expect(inviteCollaboratorsModalDataService.reinvite).toHaveBeenCalled();
    expect($scope.showReinviteSuccess).toBe(true);
}

}

我做错了什么或我错过了什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

好的,所以我错过了一个在我的规范文件中添加$ scope.settingsData的变量,这就是为什么我的规范代码没有解析到第一个&#39; if&#39;控制器的循环。通过添加&#39; reInviteChecked来实现它:true&#39;在:

$scope.settingsData = {
    userSettingsDetails: [{email: 'abc@xyz.com', reInviteChecked: true}]
};