Jasmine测试用例错误'间谍被称为' [英] Jasmine test case error 'Spy to have been called'

查看:118
本文介绍了Jasmine测试用例错误'间谍被称为'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为下面的角度函数编写茉莉花测试用例,并且获得测试用例失败消息预期的间谍[对象对象]被调用。

I'm writing jasmine test case for below angular function and getting test case failed message "Expected spy [object Object] to have been called".

    $scope.displayTagModelPopup = function() {
        var dialogOptions = {
            templateUrl: 'views/mytags.html',
            controller: 'TagsCtrl',
            size: 'lg',
            resolve: {
                tagsAvailable: function() {
                    return $scope.availableTags;
                }
            }
        };

        ModalDialogFactory.showDialog(dialogOptions).then(function(result) {
            $scope.selectedFields = [];
            $scope.selectedFieldIds = [];

            angular.forEach(result, function(tag) {
                $scope.selectedFields.push(tag);
                $scope.selectedFieldIds.push(tag.objectId);
            });
        });
    };

我的Jasmine测试用例

My Jasmine Test case

it('should call displayTagModelPopup', function() {
    var dialogOptions = {
        templateUrl: 'views/mytags.html',
        controller: 'TagsCtrl',
        size: 'lg',
        tagsAvailable: [{
            objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
            dataFieldName: "author",
            shortNamePath: "$.author",
            templates: ["HaM sheet"]
        }]
    };
    var spy = jasmine.createSpy(modalDialogFactory, 'showDialog').and.callFake(function(data) {
        $scope.tags = [{
            objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
            dataFieldName: "author",
            shortNamePath: "$.author",
            templates: ["HaM sheet"]
        }];
        return $scope.tags;
    });

    $scope.displayTagModelPopup();
    $scope.$digest();
    expect(spy).toHaveBeenCalled();

});

并获得以下错误
已调用的预期间谍[对象]。
错误:已经调用了预期的间谍[对象]。

And getting the below error "Expected spy [object Object] to have been called. Error: Expected spy [object Object] to have been called."

我的测试用例中有什么问题?我错过了什么吗?

what is the issue in my test case?am i missing anything?

提前致谢!!!

已编辑:
更改了我的Jasmine测试用例如下所示获取不同的消息''undefined'不是函数(评估'ModalDialogFactory.showDialog(dialogOptions).then')'

Edited: Changed my Jasmine test case like below getting different message ''undefined' is not a function (evaluating 'ModalDialogFactory.showDialog(dialogOptions).then')'

尝试是否定义了ModelDialogFactory,但是成功定义了ModalDialogFactory.showDialog方法。
只有在调用方法'$ scope.displayTagModelPopup();'

Tried whether ModelDialogFactory is defined or not but ModalDialogFactory.showDialog method is defined successfully. getting test case failed only when call the method '$scope.displayTagModelPopup();'

it('should call displayTagModelPopup', function() {

    spyOn(ModalDialogFactory, 'showDialog').and.callFake(function() {
        $scope.tags = [{
            objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
            dataFieldName: "author",
            shortNamePath: "$.author",
            templates: ["HaM sheet"]
        }];
        return $scope.tags;
    });
    var dialogOptions = {
        templateUrl: 'views/mytags.html',
        controller: 'TagsCtrl',
        size: 'lg',
        tagsAvailable: [{
            objectId: "c647abc7-f651-4df6-880d-cf9fb69cdcb0",
            dataFieldName: "author",
            shortNamePath: "$.author",
            templates: ["HaM sheet"]
        }]
    };
    //expect(ModalDialogFactory).toBeDefined();
    //expect(ModalDialogFactory.showDialog).toBeDefined();

    $scope.displayTagModelPopup();
    $scope.$digest();

});


推荐答案

如果你想监视现有对象的方法你应该使用 spyOn helper( docs ):

If you want to spy on a method of existing object you should use spyOn helper (docs):

spyOn(modalDialogFactory, 'showDialog').and.callFake(...);

作为第一个参数,它应用一个对象,并作为方法的第二个名称。它用间谍对象替换现有对象的方法。

As a first parameter it applies an object, and as the second - name of the method. It replaces an existing object's method with a spy object.

要检查是否调用了间谍,你应该将它传递给 expect()

To check if spy was called you should pass it to expect():

expect(modalDialogFactory.showDialog).toHaveBeenCalled();

您使用的助手 jasmine.createSpy()是创建一个裸间谍,它可以作为回调传递以确保它被调用( docs )。 jasmine.createSpy()仅应用一个参数,该参数是要在测试结果中显示的间谍的名称,这就是为什么没有任何间谍附加到您的对象。

The helper you use jasmine.createSpy() is to create a "bare" spy, which could be passed as a callback to ensure it will be called (docs). jasmine.createSpy() only applies one parameter which is the name of the spy to be shown in test results, this is why there is no any spy attached to your object.

更新:

你在第一个代码段中有一行:

You have a line in the first snippet:

ModalDialogFactory.showDialog(dialogOptions).then(function(result) { ... });

你可以看到 showDialog()之后是然后是()。我假设原始的 showDialog()返回一个带有方法然后()的对象,也许它是一个Promise。但是在您的测试中,当您监视 showDialog()方法时,从 callFake()您不会返回任何内容 then()方法,因此错误表明方法未定义。间谍完全取代了您的原始方法,并与 callFake()一起重新创建原始行为。

and as you can see showDialog() is followed by then(). I assume that original showDialog() returns some object with a method then(), maybe it's a Promise. But in your test when you spy on a showDialog() method, from callFake() you do not return anything with then() method, therefore the error states that a method is undefined. Spy completely replaced your original method and in conjunction with callFake() it recreates original behavior.

所以来自 callFake()你应该返回一个模仿Promise的东西,例如:

So from callFake() you should return something that imitates a Promise, for example:

spyOn(ModalDialogFactory, 'showDialog').and.callFake(function() {
    // ....
    return {
        then: function (cb) {
             cb($scope.tags);
        }
    };
});

这里我返回的对象有然后()方法,当用函数作为参数调用它时,这个函数就像一个回调一样用 $ scope.tags 值解析,可以在里面使用这个回调。

Here I return an object which has then() method, and when it's called with a function as a parameter, then this function acting like a callback is resolved with a $scope.tags value, which could be used inside this callback.

这篇关于Jasmine测试用例错误'间谍被称为'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆