Test a function passed as argument to another function in Jasmine

佐手、 提交于 2019-12-12 04:33:02

问题


I am getting below error(which is OK!) when called tohaveBeenCalledWith:

spy showError to have been called with [ '33' ] but actual calls were [ Function, 403 ]

Is there any way I can test Function that the function called with?

Assuming that the argument Function is testFun, How can I test if $window.location.href hascorrect value applied

function testFun(errorStatus) {
            switch (errorStatus) {
                case 401:
                    $window.location.href = url1;
                    break;
                case 403:
                    $window.location.href = url2;
                    break;             
                default:
                    console.log('Something went wrong');

            }
        }

回答1:


Yes. You can do that using jasmine.any

Quoting the documentation example:

expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function));

Hope that helps!



来源:https://stackoverflow.com/questions/41376256/test-a-function-passed-as-argument-to-another-function-in-jasmine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!