Ben*_*ing 26 javascript unit-testing jasmine
在我使用的所有测试框架中,有一个可选参数来指定您自己的自定义错误消息.
这可能非常有用,我找不到用茉莉花开箱即用的方法.
我有其他3位开发人员问我这个确切的功能,当谈到茉莉花时,我不知道该告诉他们什么.
是否可以在每个断言上指定自己的自定义错误消息?
Xot*_*bu4 11
向所有匹配器(toBe,toContain和其他)添加了可选参数,因此您可以使用:
expect(true).toBe(false, 'True should be false').
Run Code Online (Sandbox Code Playgroud)
然后在输出中它将如下所示:
Message:
Expected true to be false, 'True should be false'.
Run Code Online (Sandbox Code Playgroud)
提交链接(文档中未对此进行描述):https: //github.com/ronanamsterdam/DefinitelyTyped/commit/ff104ed7cc13a3eb2e89f46242c4dbdbbe66665e
如果您查看jasmine源代码,您将看到无法从匹配器外部设置消息.例如toBeNaN匹配器.
/**
* Matcher that compares the actual to NaN.
*/
jasmine.Matchers.prototype.toBeNaN = function() {
this.message = function() {
return [ "Expected " + jasmine.pp(this.actual) + " to be NaN." ];
};
return (this.actual !== this.actual);
};
Run Code Online (Sandbox Code Playgroud)
如您所见,消息被硬编码到匹配器中,并在您调用匹配器时进行设置.我能想出自己的消息的唯一方法就是编写像这里描述的匹配器
紧随其后的链式调用withContext()expect()。例子:
expect(myValue)
.withContext("This message will be printed when the expectation doesn't match")
.toEqual({foo: 'bar'});
Run Code Online (Sandbox Code Playgroud)
此问题跟踪使用.because()机制实现自定义错误消息的兴趣.
与此同时,avrelian创建了一个很好的库,它使用一种since()机制实现自定义错误消息jasmine-custom-message.
| 归档时间: |
|
| 查看次数: |
6296 次 |
| 最近记录: |