简体   繁体   English

如何测试方法是否只被调用一次而不是第二次调用Jasmine?

[英]How to test if method has been called only once and not the second time in Jasmine?

I am testing mechanism that should call method once and prevent any subsequent calls with jasmine, I can see by attaching breakpoints that method is NOT being called second time however jasmine test fails. 我正在测试应该调用方法一次的机制并阻止任何后续调用jasmine,我可以通过附加断点看到方法未被第二次调用但是jasmine测试失败。 I would assume it has to do with spy not being designed to be used for multiple checks. 我认为它与spy没有被设计用于多次检查有关。
What would be proper solution to given situation? 什么是适当的解决方案?

JSfiddle of Code that is being tested I could not figure out how to do jasmine test jsfiddle properly (Jasmine version I am using is 1.3.1 while test template is on 1.2.0). 正在测试的JSfiddle of Code我无法弄清楚如何正确地执行茉莉花测试jsfiddle(我使用的Jasmine版本是1.3.1而测试模板是1.2.0)。

Test looks like this: 测试看起来像这样:

 it("Invoking OnPreQuery will add event listener for OnTheMoveViewPreLoad event. Triggering OnTheMoveViewPreLoad twice will call getChildrenForMasterRecordList only first time", function () {
            AddTreeSettingsObjectToBC({ bc: bc, Tree: { IncludeChildren: true} });
            ComposeMockPageObjWithObservableFieldsWithChildren();
            var preQuerySpy = spyOnEvent(onTheMove.PageDataRoles, 'OnPreQuery');
            $(onTheMove.PageDataRoles).trigger('OnPreQuery', { knockoutContextName: 'bc' });
            expect('OnPreQuery').toHaveBeenTriggeredOn(onTheMove.PageDataRoles);
            expect(preQuerySpy).toHaveBeenTriggered();
            var getChildrenForMasterRecordListSpy = spyOn(window, 'getChildrenForMasterRecordList');
            $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' });
            expect(getChildrenForMasterRecordListSpy).toHaveBeenCalled();
            $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' });
            expect(getChildrenForMasterRecordListSpy).not.toHaveBeenCalled();
        });

Code that is being tested: HTML 正在测试的代码:HTML

<div data-role="page"></div>

Javascript 使用Javascript

var onTheMove = function(){};
$.extend(onTheMove,{
    NullValue : "null",
    PageDataRoles : 'div[data-role="page"], div[data-role="dialog"]',
    OnTheMovePrefix : 'OnTheMove_'
    });

$(document).on('OnPreQuery', onTheMove.PageDataRoles, function (e, options) {
    var isChildAttachmentQueued = true;
    var knockoutContextName = options.knockoutContextName;
    if (TreeEnabled(knockoutContextName)) {
        var isModelReadyToAttachChildren = function () {
            var isReady = false;
            if (PageObj[knockoutContextName] != undefined) {
                isReady = (PageObj[knockoutContextName]().length > 0) && isChildAttachmentQueued;
            }

            return isReady;
        };
        var treeSettings = eval(knockoutContextName).Tree;
        treeSettings.knockoutContextName = knockoutContextName;
        $(onTheMove.PageDataRoles).on('OnTheMoveViewPreLoad', function (e, options) {
            if (isModelReadyToAttachChildren()) {
                getChildrenForMasterRecordList({
                    parentTable: eval(knockoutContextName).primaryTableName,
                    knockoutContextName: treeSettings.knockoutContextName,
                    parentIdColumn: treeSettings.ParentIdColumn,
                    masterIdColumn: treeSettings.MasterIdColumn
                });
                isChildAttachmentQueued = false;
            }
        });
    }
});
function getChildrenForMasterRecordList(options) {
    console.log('beep');
}

Figured it out myself, spy has property callCount that auto-increments by one on each call. 我自己想出来,间谍有一个属性callCount ,每次调用都会自动递增1。

    it("Invoking OnPreQuery will add event listener for OnTheMoveViewPreLoad event. Triggering OnTheMoveViewPreLoad twice will call getChildrenForMasterRecordList only first time", function () {
        AddTreeSettingsObjectToBC({ bc: bc, Tree: { IncludeChildren: true} });
        ComposeMockPageObjWithObservableFieldsWithChildren();
        var preQuerySpy = spyOnEvent(onTheMove.PageDataRoles, 'OnPreQuery');
        $(onTheMove.PageDataRoles).trigger('OnPreQuery', { knockoutContextName: 'bc' });
        expect('OnPreQuery').toHaveBeenTriggeredOn(onTheMove.PageDataRoles);
        expect(preQuerySpy).toHaveBeenTriggered();
        var getChildrenForMasterRecordListSpy = spyOn(window, 'getChildrenForMasterRecordList');
        $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' });
        expect(getChildrenForMasterRecordListSpy).toHaveBeenCalled();
        $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' });
        expect(getChildrenForMasterRecordListSpy.callCount).toEqual(1);
    });

as per comment 根据评论

in Jasmine 2.0 its 在Jasmine 2.0中

expect(object.func.calls.count()).toBe(1);

toHaveBeenCalledTimes(1) toHaveBeenCalledTimes(1)

now makes this much easier. 现在让这更容易。

 expect(yourSpy).toHaveBeenCalledTimes(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试是否在 jasmine 中调用了该方法? - How do I test that the method has been called in jasmine? 茉莉花:如何测试GET请求中是否已调用正确的URL - Jasmine: How to test if the correct URL has been called in a GET request 使用Jasmine如何检查是否已调用本地方法? - Using Jasmine how to check if a local method has been called? Jasmine-成功从Ajax内部调用DOM元素的测试方法 - Jasmine - Test method has been called on DOM element from inside Ajax success 如何模拟茉莉花中其他服务方法中已调用的$ http.post方法? - How to mock $http.post method that has been called in other service method in jasmine? 您需要间谍来测试是否已在Jasmine中调用函数吗? - Do you need spies to test if a function has been called in Jasmine? Javascript单元测试Jasmine /单元测试-如何测试是否已调用私有函数? - Javascript Unittesting Jasmine / Unit Testing - how do I test whether a private function has been called? 如何断言我的Jasmine测试中调用了一种方法 - How to assert that a method is called in my Jasmine test 如果 function 在 1 秒内被调用 50 次,则仅运行一次 - Run the function only once if its been called 50 times in 1 second 如何检查方法是否已被称为茉莉花单元测试 - how to check if methods have been called jasmine unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM