如果使用avajs调用方法,则断言为true

时间:2017-01-17 18:59:23

标签: javascript unit-testing reactjs ava

我正在尝试使用react,ava等创建单元测试。我遇到了创建简单单元测试以检查方法是否被调用的问题。如果调用了方法,我的测试应该通过。但是,当我检查代码覆盖率时,我收到一条消息说"功能未涵盖"。下面是我用来测试它的代码。

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');
 });

我做错了什么?

1 个答案:

答案 0 :(得分:1)

经过几次尝试,我能够弄清楚:

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);
})