柴的断言,期待和应该有什么区别?

Man*_*anu 147 javascript mocha.js chai

有什么区别assert,expectshould什么时候使用什么?

assert.equal(3, '3', '== coerces values to strings');

var foo = 'bar';

expect(foo).to.equal('bar');

foo.should.equal('bar');
Run Code Online (Sandbox Code Playgroud)

Lou*_*uis 277

的差异记录都在这里.

这三个接口呈现不同类型的执行断言.最终,他们执行相同的任务.有些用户比另一种更喜欢一种风格.话虽如此,还有一些值得强调的技术考虑因素:

  1. assert和expect接口不会修改Object.prototype,而应该修改.因此,在您不能或不想改变的环境中,它们是更好的选择Object.prototype.

  2. assert和expect接口支持几乎所有地方的自定义消息.例如:

    assert.isTrue(foo, "foo should be true");
    expect(foo, "foo should be true").to.be.true;
    
    Run Code Online (Sandbox Code Playgroud)

    如果断言失败,则消息"foo应为true"将与失败的断言一起输出.您没有机会使用should接口设置自定义消息.

(历史记录:很长一段时间,这个答案声明要获得自定义消息expect,你必须使用一种解决方法.AurélienRibon告诉我,将消息传递expect给第二个参数是有效的.因此,没有必要解决方法.我无法找到哪个版本的Mocha开始为此消息提供支持,也无法找到第一次记录哪个版本的文档.)

需要注意的是assert.isTrue(foo),expect(foo).to.be.truefoo.should.be.true所有的输出,如果你不使用自定义消息下面,和foo === 1:

    AssertionError: expected 1 to be true
Run Code Online (Sandbox Code Playgroud)

因此,虽然期望和界面阅读更好,但是当断言失败时,并不是说一个界面比另一个更自然地提供信息.这个消息,这是所有三个接口是相同的,不会告诉你什么正是你在测试中,只有你得到的值1,但是你想true.如果您想知道您在测试什么,则需要添加消息.

  • 请注意,您也可以执行`expect(foo).to.equal(true,"foo应该为true");` (8认同)

Adé*_*gun 11

我希望这个简单的例子可以使它们之间的区别清晰

断言

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');
Run Code Online (Sandbox Code Playgroud)

在所有情况下,断言样式都允许您在断言语句中包括一个可选消息作为最后一个参数。如果您的断言没有通过,这些将包含在错误消息中。

请注意, 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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

当与非描述性主题(如布尔值或数字)一起使用时,这非常方便。

应该

应该的样式允许使用与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);
Run Code Online (Sandbox Code Playgroud)

预期与应有之间的差异

首先,请注意,expected require只是对Expecting函数的引用,而使用should require时,该函数正在执行。

var chai = require('chai')
, expect = chai.expect
, should = chai.should();
Run Code Online (Sandbox Code Playgroud)

预期接口提供的功能作为链接你的语言断言的起点。它适用于node.js和所有浏览器。

接口扩展Object.prototype中提供单一的吸气剂为你的语言断言的起点。它适用于node.js和除Internet Explorer之外的所有现代浏览器。