assert
,expect
和should
和之间的区别是什么?何时使用?
assert.equal(3, '3', '== coerces values to strings');
var foo = 'bar';
expect(foo).to.equal('bar');
foo.should.equal('bar');
差异记录在那里。
这三个接口呈现执行断言的不同样式。最终,他们执行相同的任务。一些用户更喜欢一种风格。话虽如此,还有一些技术方面的注意事项值得强调:
-
assert和Expect接口不会修改
Object.prototype
,而应该修改。因此,在您无法或不想更改的环境中,它们是更好的选择Object.prototype
。 -
assert和Expect接口几乎在所有地方都支持自定义消息。例如:
assert.isTrue(foo, "foo should be true"); expect(foo, "foo should be true").to.be.true;
如果断言失败,将与失败的断言一起输出消息“ foo应该为真”。您没有机会通过should接口设置自定义消息。
(Historical note: for a long time this answer stated that to get a custom message with expect
, you'd have to use a workaround. Aurélien Ribon informed me that passing a message to expect
as a second parameter works. Consequently, there is no need for a workaround. I've not been able to find which version of Mocha started providing support for this message, nor have I been able to find which version of the documentation documented it for the first time.)
Note that assert.isTrue(foo)
, expect(foo).to.be.true
and foo.should.be.true
all output the following if you do not use a custom message, and foo === 1
:
AssertionError: expected 1 to be true
因此,虽然Expect和应该接口更易于阅读,但是当断言失败时,这不像一个接口比另一个接口更自然地提供信息。该消息对于所有三个接口都是相同的,它不会告诉您正在测试的是什么,只是告诉您所获得的价值是1
您想要的true
。如果您想知道要测试的内容,则需要添加一条消息。
我希望这个简单的例子可以使它们之间的区别清晰
断言
var assert = require('chai').assert
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
assert.typeOf(foo, 'string'); // without optional message
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
assert.equal(foo, 'bar', 'foo equal `bar`');
assert.lengthOf(foo, 3, 'foo`s value has a length of 3');
assert.lengthOf(beverages.tea, 3, 'beverages has 3 types of tea');
在所有情况下,断言样式都允许您在断言语句中包括可选消息作为最后一个参数。如果您的断言没有通过,这些将包含在错误消息中。
请注意,
expect并应使用可链接的语言来构造断言,但是它们在初始构造断言的方式上有所不同。在应有的情况下,还存在一些警告和克服这些警告的其他工具。
期望
var expect = require('chai').expect
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);
Expect允许您包含任意消息,以放在可能发生的任何失败的断言之前。
var answer = 43;
// AssertionError: expected 43 to equal 42.
expect(answer).to.equal(42);
// AssertionError: topic [answer]: expected 43 to equal 42.
expect(answer, 'topic [answer]').to.equal(42);
当与非描述性主题(如布尔值或数字)一起使用时,这非常方便。
应该
应该的样式允许与Expect接口相同的可链接的断言,但是它使用should属性扩展每个对象以启动链。与Internet Explorer一起使用时,此样式存在一些问题,因此请注意浏览器的兼容性。
var should = require('chai').should() //actually call the function
, foo = 'bar'
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
beverages.should.have.property('tea').with.lengthOf(3);
预期与应有之间的差异
首先,请注意,require require只是对Expecting函数的引用,而使用should require时,该函数正在执行。
var chai = require('chai')
, expect = chai.expect
, should = chai.should();
该预期接口提供的功能作为链接你的语言断言的起点。它适用于node.js和所有浏览器。
本应接口扩展Object.prototype中提供单一的吸气剂为你的语言断言的起点。它适用于node.js和除Internet Explorer之外的所有现代浏览器。
文章标签:chai , javascript , mocha
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!