not.custom_matcher错误 - 茉莉花 [英] not.custom_matcher error - Jasmine

查看:126
本文介绍了not.custom_matcher错误 - 茉莉花的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 jasmine 中创建了自定义匹配器,以验证元素是否有类。这是我在 beforeEach()函数中放置的代码,用于定义自定义匹配器:

I have created a custom matcher in jasmine to verify if an element has a class or not. This is the code that I have placed in the beforeEach() function to define the custom matcher:

beforeEach(function() {

    jasmine.addMatchers({
        toHaveClass: function() {
            return {
                compare: function(actual, expected) {
                    return {
                        pass: actual.getAttribute('class').then(function(classes) {
                            return classes.split(' ').indexOf(expected) !== -1;
                        })
                    }
                }
            }
        }
    });

    this.driver = new webdriver.Builder().forBrowser('firefox').build();
    this.driver.manage().window().maximize();
    this.driver.get('http://localhost:8000/');
});

然后在函数 toHaveClass 正常工作,但当我将它与 .not 方法结合使用时,我得到了一个非常奇怪的错误。这是我遇到错误的代码的一部分:

Then in the it function toHaveClass works properly, but when I combine it with the .not method, I get a really weird mistake. This is the part of the code where I am having the error:

it('should not create conflicts between the headers', function() { 
    this.driver.manage().window().setSize(767, 632);
    this.driver.findElement(webdriver.By.className('navbar-toggle')).click();
    var headerBar = this.driver.findElement(webdriver.By.className('navbar-collapse'));
    this.driver.manage().window().setSize(1000, 632).then(function() {
        expect(headerBar).not.toHaveClass('in');      
    });
});

你知道导致测试崩溃的问题是什么吗?在此先感谢您的回复!

Do you know what could be the issue that causes the test to crash? Thanks in advance for your replies!

推荐答案

对我来说,它看起来像你的实际结束 undefined null ,因为你避免返回 -1

To me it looks like your actual ends undefined or null, because you avoid to return -1.

使用行返回classes.split('')。indexOf(预期)!== -1; 您只返回找到的值,因此未找到找不到的值。

With the line return classes.split(' ').indexOf(expected) !== -1; you only return on found values, so not found values get not returned.

您可以尝试的一个实验是删除条件!== -1 ,因此还会返回未找到。但是,我不知道这方面的影响;它可能只是在另一个部分打破或(因为 .not 正在寻找某种 false -return),它仍然可能无法工作,甚至你的 expect()。toHaveClass()的积极情况可能会在失败时结束。

One experimental thing you could try is to remove the condition !== -1, so also a not found gets returned. However, I don't know the side impacts of this; it could break just at another part or (as .not is looking for some kind of false-return), it still might not work and even your positive cases with expect().toHaveClass() could end successful when they should fail.

我看到更安全的选项是创建另一个自定义匹配器 toNotHaveClass 然后仅返回 === -1 ,所以只有找不到值。基本上这个:

The safer option I see is to create another custom matcher toNotHaveClass and then return only on === -1, so only if a value isn't found. Basically this:

jasmine.addMatchers({
    toNotHaveClass: function() {
        return {
            compare: function(actual, expected) {
                return {
                    pass: actual.getAttribute('class').then(function(classes) {
                        return classes.split(' ').indexOf(expected) === -1;
                    })
                }
            }
        }
    }
});

这篇关于not.custom_matcher错误 - 茉莉花的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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