【问题标题】:assert true if method has been called using avajs如果使用 avajs 调用了方法,则断言 true
【发布时间】:2017-01-17 18:59:22
【问题描述】:

我正在尝试使用 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');
 });

我做错了什么?

【问题讨论】:

    标签: javascript unit-testing reactjs ava


    【解决方案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);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多