【问题标题】:What is the difference between “assert”, “expect”, and “should” in Chai?Chai 中的“assert”、“expect”和“should”有什么区别?
【发布时间】:2014-02-19 05:11:20
【问题描述】:

assertexpectshould 有什么区别?什么时候用什么?

assert.equal(3, '3', '== coerces values to strings');
    
var foo = 'bar';
    
expect(foo).to.equal('bar');
    
foo.should.equal('bar');

【问题讨论】:

    标签: javascript mocha.js chai


    【解决方案1】:

    我希望这个简单的例子能说明它们之间的区别

    断言

    var assert = require('chai').assert
    const foo = 'bar'
    const 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 和 should 使用可链接的语言来构造断言,但它们在断言最初构造的方式上有所不同。在 should 的情况下,还有一些注意事项和额外的工具可以克服这些注意事项。

    期待

    var expect = require('chai').expect
    const foo = 'bar'
    const 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);
    

    这在与非描述性主题(例如布尔值或数字)一起使用时会派上用场。

    应该

    should 样式允许与期望接口相同的可链接断言,但是它使用 should 属性扩展每个对象以启动链。此样式在与 Internet Explorer 一起使用时存在一些问题,因此请注意浏览器兼容性。

    var should = require('chai').should() //actually call the function
    const foo = 'bar'
    const 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);
    

    期望和应该的区别

    首先,请注意,expect require 只是对 expect 函数的引用,而 should require 函数正在执行。

    var chai = require('chai')
    const expect = chai.expect
    const should = chai.should();
    

    expect 接口提供了一个函数作为链接语言断言的起点。它适用于 node.js 和所有浏览器。

    should 接口扩展了 Object.prototype 以提供单个 getter 作为语言断言的起点。它适用于 node.js 和除 Internet Explorer 之外的所有现代浏览器。

    【讨论】:

      【解决方案2】:

      区别是documented there

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

      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;
        

        如果断言失败,则消息“foo should be true”将与失败的断言一起输出。您没有机会使用 should 界面设置自定义消息。

      (历史记录:很长一段时间以来,这个答案都表明要使用expect 获取自定义消息,您必须使用一种解决方法。Aurélien Ribon 告诉我将消息传递给expect 作为第二个参数有效。因此,不需要解决方法。我无法找到哪个版本的 Mocha 开始为此消息提供支持,也无法找到第一次记录它的文档版本.)

      注意assert.isTrue(foo)expect(foo).to.be.truefoo.should.be.true如果不使用自定义消息都会输出以下内容,foo === 1

          AssertionError: expected 1 to be true
      

      因此,虽然期望和应该接口更易于读取,但当断言失败时,并不是一个接口比另一个接口更自然地提供信息。此消息对于所有三个接口都是相同的,它并没有告诉您 什么 您到底在测试什么,只是您得到的值是 1,但您想要的是 true。如果你想知道你在测试什么,你需要添加一条消息。

      【讨论】:

      • 注意你也可以expect(foo).to.equal(true, "foo should be true");
      • 使用最新版本的 mocha,我无法使用expect 显示任何自定义消息
      • @Mirko Mocha 的版本在这里并不重要。你用的是最新的 Chai 吗?
      • 对我来说也一样,在香草快递 (4.16.3)、摩卡 (5.1.1)、柴 (4.1.2)、柴-http (4.0.0) 项目上。使用命令 mocha 运行并测试失败时,自定义消息不会出现在任何地方。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2012-11-18
      • 2017-03-04
      • 2011-02-26
      • 1970-01-01
      相关资源
      最近更新 更多