gpt4 book ai didi

javascript - 在 Mocha 中使用 Sinon 读取断言控制台消息

转载 作者:行者123 更新时间:2023-11-30 15:15:48 25 4
gpt4 key购买 nike

我正在尝试测试网络组件。如果设置了无效的属性值,此 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.calledWith。然后我收到了这个错误:

sandbox.fakes.calledWith is not a function

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

感谢任何帮助。

最佳答案

你的位置

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

没有意义,应该是

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

关于javascript - 在 Mocha 中使用 Sinon 读取断言控制台消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44483717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com