我正在尝试测试Web组件。如果设置了无效的属性值,则此Web组件会将警告消息写入控制台。目前,我有以下内容:

import { expect } from 'chai';
import { mount } from 'avoriaz';

import MyComponent from '../src/my-component.vue';

const sinon = require('sinon');

describe('my-component.vue', function() {
  let sandbox = null;

  beforeEach(function() {
    sandbox = sinon.sandbox.create();
    sandbox.stub(console, 'warn');
  });

  afterEach(function() {
    sandbox.restore();
  });

  it('should show warning message in console', function() {
    let wrapper = mount(MyComponent, { propsData: { start:-1 } });  // start has to be positive.
    let result = sandbox.calledWith('WARNING!');
    expect(result).to.equal(true);
  });
});


运行这些测试时,会引发以下异常:

sandbox.calledWith is not a function


然后,我尝试使用sandbox.fakes.callWith代替。然后,我收到此错误:

sandbox.fakes.calledWith is not a function


我究竟做错了什么?如何测试是否将控制台消息写入控制台行?问题是,如果我删除了sandbox.stub(console,'warn');行,我可以看到写到实际控制台窗口的行。因此,我知道它正在生成(按预期)。我只是不知道如何测试。

任何帮助表示赞赏。

最佳答案

你有的地方

let result = sandbox.calledWith('WARNING!');


没有道理,应该

let result = console.warn.calledWith('WARNING!');

10-01 16:28
查看更多