有什么区别expect(something).toBe(true)
,expect(something).toBeTruthy()
和expect(something).toBeTrue()
?
请注意,这toBeTrue()
是一个自定义匹配器,介绍在jasmine-matchers
其他有用和方便的匹配器,如toHaveMethod()
或toBeArrayOfStrings()
.
问题是通用的,但是,作为一个真实的例子,我正在测试一个元素是否显示在protractor
.在这种情况下我应该使用哪个匹配器?
expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();
Run Code Online (Sandbox Code Playgroud) 有很多文档显示如何将匹配器添加到Jasmine规范(例如,这里).
有没有人找到一种方法将匹配器添加到整个环境中; 我想创建一组有用的匹配器,可以被任何和所有测试调用,而不是我的规范中的copypasta.
目前正在努力对源进行反向工程,但如果存在,则更喜欢一种经过验证的方法.
我在angular2项目中使用jasmine,并且在编写自定义匹配器时遇到一些麻烦.我希望能够比较两个相对复杂的对象.我发现这篇文章声称可以解决这个问题,但它只会导致一个打字稿错误,说明它无法识别jasmine Matchers
对象上的新方法.相关代码是这样的:
declare module jasmine {
interface Matchers {
toBeNumeric(): void;
}
}
Run Code Online (Sandbox Code Playgroud)
另一篇文章给出了一个类似但略有不同的解决方案,它给出了同样的错
declare namespace jasmine {
interface Matchers {
toHaveText(expected: string): boolean;
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个
let m: jasmine.Matchers = expect(someSpy.someMethod).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
类型'jasmine.Matchers'不能指定为'jasmine.Matchers'类型.存在两种具有此名称的不同类型,但它们是不相关的.
这似乎表明该declare namespace jasmine
语句正在创建一个新的jasmine
命名空间而不是扩展现有的命名空间.
那么如何创建我自己的打字稿,打字稿会很满意?
在Jasmine,有toBeGreaterThan
和toBeLessThan
匹配.
如果我想检查特定范围内的整数值怎么办?toBeInBetween
匹配器有什么吗?
目前,我可以通过两个单独的expect
调用解决它:
var x = 3;
expect(x).toBeGreaterThan(1);
expect(x).toBeLessThan(10);
Run Code Online (Sandbox Code Playgroud) 我有一个使用 jest 的 react/typescript 项目,其中有一个自定义匹配器,例如:
export const MyCustomMatchers = {
toBeTheSameAsRemote: function(_util: any, _customEqualityTesters: any) {
return {
compare: function(actual: Brand, expected: RemoteBrand) {
const pass: boolean = attributesMatch(actual, expected);
const message: string = pass
? 'Local matches Remote'
: 'Local does not match Remote';
return { pass, message: () => message };
}
};
}
};
Run Code Online (Sandbox Code Playgroud)
我在我的测试中通过在describe
函数内部进行引用:
beforeEach(() => {
jasmine.addMatchers(MyCustomMatchers);
});
Run Code Online (Sandbox Code Playgroud)
并在it
函数中像这样使用:
expect(localValue).toBeTheSameAsRemote(remoteValue);
Run Code Online (Sandbox Code Playgroud)
测试运行正常,但打字稿编译器无法识别匹配器,这是有道理的,因为我没有在类型系统中的任何地方定义它
Property 'toBeTheSameAsRemote' does not exist on type 'JestMatchersShape<Matchers<void, MyType[]>, Matchers<Promise<void>, …
Run Code Online (Sandbox Code Playgroud) 这是失败的测试:
describe("Checking errors", function () {
var scope = {};
beforeEach(function () {
browser.get("/#endpoint");
browser.waitForAngular();
scope.page = new MyPage();
});
it("should not show any errors", function () {
expect(scope.page.errors).toBeEmptyArray();
});
});
Run Code Online (Sandbox Code Playgroud)
这里MyPage
是一个Page对象:
var MyPage = function () {
this.errors = element.all(by.css("div.error-block b.error"))
.filter(function (elm) {
return elm.isDisplayed().then(function (value) {
return value;
});
})
.map(function (elm) {
return elm.getText();
});
};
module.exports = MyPage;
Run Code Online (Sandbox Code Playgroud)
这里errors
应该是可见的错误文本的阵列在网页上找到.
这是我们得到的错误:
Failures:
1) Checking errors should not show …
Run Code Online (Sandbox Code Playgroud) jasmine ×5
javascript ×3
testing ×3
protractor ×2
typescript ×2
assertions ×1
jestjs ×1
ts-jest ×1
types ×1