如果使用 avajs 调用了方法,则断言为真
assert true if method has been called using avajs
我正在尝试使用 React、Ava 等创建单元测试。我在创建简单的单元测试以检查是否调用了方法时遇到了问题。如果调用了该方法,我的测试应该会通过。然而,当我检查代码覆盖率时,我收到一条消息说 "function not covered"。下面是我用来测试它的代码。
import test from 'ava';
import React from 'react';
import { Cart } from 'components/Cart/CartDisplay';
import { shallow } from 'enzyme';
let props;
test.beforeEach(() => {
props = {
popError: () => {},
message: '',
count: 2,
displayCart:() => {},
onClose:() => {}
};
});
test('renders okay?', (t) => {
shallow(
<Cart {...props} />
);
t.pass('yes');
});
test('Cart Displayed okay?', (t) => {
props.displayCart();
t.pass('yes');
});
我做错了什么?
经过几次尝试,我弄明白了:
import test from 'ava';
import React from 'react';
import { Cart } from 'components/Cart/CartDisplay';
import { shallow,mount } from 'enzyme';
import sinon from 'sinon';
import {expect} from 'chai';
let props;
test.beforeEach(() => {
props = {
popError: () => {},
message: '',
count: 2,
displayCart:() => {},
onClose:() => {}
};
});
test('Cart Display called?', t => {
sinon.spy(Cart.prototype, 'cartDisplay');
const wrapper = mount(<BannerMessage />);
expect(Cart.prototype.componentDidMount.calledOnce).to.equal(true);
})
我正在尝试使用 React、Ava 等创建单元测试。我在创建简单的单元测试以检查是否调用了方法时遇到了问题。如果调用了该方法,我的测试应该会通过。然而,当我检查代码覆盖率时,我收到一条消息说 "function not covered"。下面是我用来测试它的代码。
import test from 'ava';
import React from 'react';
import { Cart } from 'components/Cart/CartDisplay';
import { shallow } from 'enzyme';
let props;
test.beforeEach(() => {
props = {
popError: () => {},
message: '',
count: 2,
displayCart:() => {},
onClose:() => {}
};
});
test('renders okay?', (t) => {
shallow(
<Cart {...props} />
);
t.pass('yes');
});
test('Cart Displayed okay?', (t) => {
props.displayCart();
t.pass('yes');
});
我做错了什么?
经过几次尝试,我弄明白了:
import test from 'ava';
import React from 'react';
import { Cart } from 'components/Cart/CartDisplay';
import { shallow,mount } from 'enzyme';
import sinon from 'sinon';
import {expect} from 'chai';
let props;
test.beforeEach(() => {
props = {
popError: () => {},
message: '',
count: 2,
displayCart:() => {},
onClose:() => {}
};
});
test('Cart Display called?', t => {
sinon.spy(Cart.prototype, 'cartDisplay');
const wrapper = mount(<BannerMessage />);
expect(Cart.prototype.componentDidMount.calledOnce).to.equal(true);
})