期待数组中的项目 [英] Expect item in array
问题描述
我的一个测试期望错误消息文本是多个值之一。由于 getText()
返回一个承诺我不能使用 toContain()
jasmine matcher。以下不起作用,因为量角器
( jasminewd
引擎盖下)无法解决第二部分中的承诺匹配器, toContain()
在这种情况下:
One of my test expects an error message text to be one of multiple values. Since getText()
returns a promise I cannot use toContain()
jasmine matcher. The following would not work since protractor
(jasminewd
under-the-hood) would not resolve a promise in the second part of the matcher, toContain()
in this case:
expect(["Unknown Error", "Connection Error"]).toContain(page.errorMessage.getText());
问题:有没有办法检查元素是否在带有jasmine +量角器的数组,其中一个元素是一个承诺?
Question: Is there a way to check if an element is in an array with jasmine+protractor where an element is a promise?
换句话说,我正在寻找的倒数toContain()
以便 expect()
隐式解决传入的承诺。
In other words, I'm looking for inverse of toContain()
so that the expect()
would implicitly resolve the promise passed in.
作为解决方法,我可以使用显式解决承诺然后()
:
As a workaround, I can explicitly resolve the promise with then()
:
page.errorMessage.getText().then(function (text) {
expect(["Unknown Error", "Connection Error"]).toContain(text);
});
我不确定这是不是最好的选择。我也可以使用基于第三方的解决方案,如 jasmine-matchers
。
I'm not sure if this is the best option. I would also be okay with a solution based on third-parties like jasmine-matchers
.
例如,这种断言存在于Python中:
As an example, this kind of assertion exists in Python:
self.assertIn(1, [1, 2, 3, 4])
推荐答案
看起来你需要一个自定义匹配器。取决于您使用的Jasmine版本:
Looks like you need a custom matcher. Depending on the version of Jasmine you are using:
使用Jasmine 1:
this.addMatchers({
toBeIn: function(expected) {
var possibilities = Array.isArray(expected) ? expected : [expected];
return possibilities.indexOf(this.actual) > -1;
}
});
使用Jasmine 2:
this.addMatchers({
toBeIn: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var possibilities = Array.isArray(expected) ? expected : [expected];
var passed = possibilities.indexOf(actual) > -1;
return {
pass: passed,
message: 'Expected [' + possibilities.join(', ') + ']' + (passed ? ' not' : '') + ' to contain ' + actual
};
}
};
}
});
您必须在 beforeEach <中执行此操作/ code>每个
描述
块的部分将用于。
您的期望看起来像:
expect(page.errorMessage.getText()).toBeIn(["Unknown Error", "Connection Error"]);
这篇关于期待数组中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!