vue.nextTick误报

时间:2019-06-11 11:38:00

标签: vue.js mocha sinon-chai

上下文:

const Constructor = Vue.extend(MyComponent);
function createComponent() {
    vm = new Constructor({
      props,
    }).$mount();

    return vm;
  }

问题: 在测试中,我发现

vm.$nextTick().then(() => {
 expect(result).to.equal(expectedResult);
})

vm.$nextTick().then(() => {
 expect(result).to.not.equal(expectedResult);
})

都过去了。 如何摆脱这种情况? aync是否会以某种方式等待真相的过去?

1 个答案:

答案 0 :(得分:0)

首先:仅仅因为您可以使用.not否定任何断言,但这并不意味着您应该这样做。拥有权利的同时也被赋予了重大的责任。通常最好断言一个预期的输出已经产生,而不是断言没有产生无数意外的输出之一。

Equal表示目标严格(===)等于给定的val。

expect(2).to.equal(2); // Recommended
expect(2).to.not.equal(1); // Not recommended

好吧,现在,根据您的情况,您可能希望在链的前面添加.deep,以使用深度相等代替:

// Target object deeply (but not strictly) equals `{a: 1}`
expect({a: 1}).to.deep.equal({a: 1});
expect({a: 1}).to.not.equal({a: 1});

// Target array deeply (but not strictly) equals `[1, 2]`
expect([1, 2]).to.deep.equal([1, 2]);
expect([1, 2]).to.not.equal([1, 2]);

解释为什么很容易:

1 === 1 // These are primitives, they hold the same reference - they are strictly equal
1 == '1' // These are two different primitives, through type coercion they hold the same value - they are loosely equal
{ a: 1 } !== { a: 1 } // These are two different objects, they hold different references and so are not strictly equal - even though they hold the same values inside
{ a: 1 } != { a: 1 } // They have the same type, meaning loose equality performs the same check as strict equality - they are still not equal.

猜“ to.equal”使用“ deep-eql”算法=> package

var deepEql = require("deep-eql");
deepEql({ a: 1 }, { a: 1 }) === true // deepEql can determine that they share the same keys and those keys share the same values, therefore they are deeply equal!

您想进一步了解chai方法吗?他们的docs很棒。