【问题标题】:How do I get jasmine tests to ignore something on parent.window?如何让茉莉花测试忽略 parent.window 上的某些内容?
【发布时间】:2023-03-03 04:17:01
【问题描述】:

我有一个在 iframed 窗口中运行的 Typescript/Angular 应用程序。在应用程序中,我使用intl-tel-input 模块来格式化和验证我们的电话号码输入。 Intl-tel-input 将“intlTelInputUtils”函数放在 $window 对象上。在没有 iframe 的情况下本地测试应用程序时,我能够成功调用类似的方法(注意周围的窗口只是为了满足 Typescript 编译器:

(<any>window).intlTelInputUtils.getExtension(phoneNumber.userInput, phoneNumber.countryCode);

并通过模拟 window.intlTelInputUtils 对象来通过测试:

$window.intlTelInputUtils = {
            'getExtension': function () { return ''; },
            'formatNumber': function () { return ''; },
            'isValidNumber': function () { return true; }
        };

但是,一旦我们在嵌入的 iframe 窗口中,这当然不起作用。 IntlTelInputUtils 现在出现在 parent.window.intl....

我尝试将方法调用更改为:

(<any>parent.window).intlTelInputUtils.getExtension(phoneNumber.userInput, phoneNumber.countryCode);

但我无法将它推送到服务器以查看它是否有效(会吗??),直到我可以通过 Jasmine 测试。我尝试将模拟更改为:

$parent.$window = {
            'intlTelInputUtils': {
                'getExtension': function () {
                    return '';
                },
                'formatNumber': function () {
                    return '';
                },
                'isValidNumber': function () {
                    return true;
                }
            }
        };

但我意识到这是不正确的。

如何在我的测试中正确地模拟 parent.window.intlTelInputUtils??

【问题讨论】:

    标签: angularjs typescript jasmine


    【解决方案1】:

    我不确定这是否能解决您的问题,但您是否尝试在该特定呼叫中添加间谍?例如:

    it('my test name', inject(function ($window) {
         spyOn($window.intlTelInputUtils, 'getExtension');
    
         ...
    
    }));
    

    而且你需要确保你在你的代码上使用$window,而不是直接使用window

    【讨论】:

    • 我没有尝试过,但问题是我必须将代码从 $window 更改为 parent.$window。那么我会监视 parent.$window 吗?
    • 我会试试的,是的。而且,如果需要,监视两者都没有问题。 :)
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多