2016-09-26 7 views
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; 
    }); 
}); 

답변

0

, 본질적으로 호출 createCustomer 그 자체 일 때 save이 (가) 스파이가 아니라 호출됩니다. 그 당시 평가 된 customerActions.createCustomer 평가가 없습니다. 생성 된 스파이는 기능에 대해 스파이를하지만 결코 상담을받지 못합니다.

아래의 테스트 세트는 원래 createCustomer 기능이 아닌 스파이 구성 요소를 제공합니다. 두 가지 테스트 케이스 모두 통과합니다.

const App =() => <div>App</div>; 
const mapStateToProps =() => { return { title: 'New Customer' }; }; 
const customerActions = { 
    createCustomer() { return { type: 'CREATE_CUSTOMER' }; } 
}; 

describe('Container:NewCustomerContainer', function() { 
    let sandbox, createCustomerStub, component; 

    beforeEach(function() { 
    sandbox = sinon.sandbox.create(); 
    createCustomerStub = sandbox.spy(customerActions, 'createCustomer'); 

    const ConnectedApp = connect(
     mapStateToProps, 
     {save: customerActions.createCustomer} 
    )(App); 

    const wrapper = mount(
     <Provider store={createStore(() => {})}> 
     <ConnectedApp /> 
     </Provider> 
    ); 
    component = wrapper.find(App); 
    }); 

    afterEach(function() { 
    sandbox.restore(); 
    }); 

    it('should pass the correct props', function() { 
    expect(component.prop('title')).to.equal('New Customer'); 
    }); 

    it('should pass createCustomer as the save prop', function() { 
    component.prop('save')(); 
    expect(createCustomerStub.called).to.be.true;⋅ 
    }); 
}); 
+0

그러나이 테스트는 연결된 구성 요소가 'save' 메소드의 올바른 작업 작성자를 통과했는지 테스트하지 않습니다. 테스트에서 전달하는 저장 메소드 만 테스트합니다. –

+0

그것이 OP의 잘못 된 테스트 케이스가있는 부분입니다. 테스트 스파이가 필요한 부분입니다. 스파이는 스파이 기능을 대체하지 않습니다. 스파이 기능이 호출 될 때 무슨 일이 일어나고 있는지를 기록하는 레코더입니다. – Season

관련 문제