2017-01-17 36 views
-1

我想創建一個單元測試使用react,ava等..我有問題創建一個簡單的單元測試,以檢查是否調用方法。如果方法被調用,我的測試應該通過。但是,當我檢查代碼覆蓋範圍時,我收到一條消息,說「功能未覆蓋」。以下是我用來測試它的代碼。聲明真如果方法已被呼叫使用avajs

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

多試幾次之後,我能弄明白:

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