0
가짜 저장소를 사용하여 연결된 구성 요소를 쉽게 테스트하여 올바른 소품이 내 프레젠테이션 구성 요소로 전달되도록 할 수 있습니다. 그러나 올바른 동작이 통과되고 있는지 테스트 할 수 없습니다. 여기 연결된 구성 요소 작업을 테스트하는 방법
내 간단한 연결 구성 요소입니다import {createCustomer} from './customerActions';
// other imports
function mapStateToProps(state) {
return {
title: 'New Customer',
};
}
export default connect(
mapStateToProps,
{save: createCustomer}
)(CustomerForm);
그리고 여기 테스트입니다 : 구성 요소는 다음과
import {createCustomer} from './customerActions';
export default connect(
mapStateToProps,
{save: createCustomer}
)(CustomerForm);
로 createCustomer
를 프로비저닝
import * as customerActions from './customerActions';
// other imports
describe('Container:NewCustomerContainer', function() {
let sandbox, createCustomerStub;
beforeEach(function() {
sandbox = sinon.sandbox.create();
createCustomerStub = sandbox.spy(customerActions, 'createCustomer'); // attempt to spy on the action that should be passed as a prop
});
afterEach(function() {
sandbox.restore();
});
function setup() {
const store = storeFake();
return mount(
<Provider store={store}>
<NewCustomerContainer>
<div>foo</div>
</NewCustomerContainer>
</Provider>
);
}
// this works fine
it('should pass the correct props', function() {
const wrapper = setup();
const component = wrapper.find(CustomerForm);
expect(component.prop('title')).to.equal('New Customer');
});
// this fails
it('should pass createCustomer as the save prop', function() {
const wrapper = setup();
const component = wrapper.find(CustomerForm);
// call the prop action to see if the correct method was called
component.prop('save')({});
// error, stub method wasn't called. The REAL createCustomer is called instead
expect(createCustomerStub.called).to.be.true;
});
});
그러나이 테스트는 연결된 구성 요소가 'save' 메소드의 올바른 작업 작성자를 통과했는지 테스트하지 않습니다. 테스트에서 전달하는 저장 메소드 만 테스트합니다. –
그것이 OP의 잘못 된 테스트 케이스가있는 부분입니다. 테스트 스파이가 필요한 부분입니다. 스파이는 스파이 기능을 대체하지 않습니다. 스파이 기능이 호출 될 때 무슨 일이 일어나고 있는지를 기록하는 레코더입니다. – Season