2015-01-10 4 views
4

반응 + 플럭스 애플리케이션의 상점에 대한 단위 테스트를 작성하고 있습니다. 나는 모의 디스패처 here를 설정의 예를 따라, 내 단위 테스트는 다음과 같습니다Dispatcher가 jest 유닛 테스트에서 콜백을 등록하지 않습니다.

ShopDispatcher.register (payload) -> 
    action = payload.action 

    switch action.type 

    when ActionTypes.BUILD_QUERY_STRING 
     WebApiUtils.fetchItems(payload) 

    when ActionTypes.RECEIVE_FILTER_RESP_DATA 
     _setItems(action.data) 

    ItemStore.emitChange() 

I :

내 item_store.coffee 파일에서
jest.dontMock "../../app/scripts/stores/item_store.coffee" 
jest.dontMock "object-assign" 

describe 'ItemStore', -> 
    ShopConstants = require "../../app/scripts/constants/shop_constants.coffee" 
    ShopDispatcher = undefined 
    ItemStore = undefined 
    callback = undefined 

    actionBuildQueryString = 
    source: "VIEW_ACTION" 
    action: 
     type: ShopConstants.ActionTypes.BUILD_QUERY_STRING 
     size: "4" 

    actionReceiveFilterRespData = 
    source: "SERVER_ACTION" 
    action: 
     type: ShopConstants.ActionTypes.RECEIVE_FILTER_RESP_DATA 
     data: {item: {} } 

    beforeEach -> 
    ShopConstants = require "../../app/scripts/constants/shop_constants.coffee" 
    ShopDispatcher = require "../../app/scripts/dispatchers/shop_dispatcher.coffee" 
    ItemStore = require "../../app/scripts/stores/item_store.coffee" 
    callback = ShopDispatcher.register.mock.calls[0][0] 

    it "registers a callback with the dispatcher", -> 
    expect(ShopDispatcher.register.mock.calls.length).toBe(1) 

, 나는 그래서 디스패처에 등록 예상치 못한 디스패처는 실제 item_store 파일에서 콜백을 등록 할 것으로 예상했다. 그러나 ShopDispatcher.register는 정의되지 않았으므로 등록되지 않았지만 그 이유는 확실하지 않습니다. 어떤 도움을 주셔서 감사합니다.

+0

(ShopDispatcher.register.mock.calls.length) .toBe (1)가 실패합니까? 나는 CoffeeScript 문법에 익숙하지 않지만, beforeEach에서 기술의 시작 부분에 선언 한 ShopDispatcher를 올바르게 덮어 쓰는 데 필요한 것이 무엇입니까? var 키워드가 없으면 범위가 무엇인지 확신 할 수 없습니다. 또는 상점에서 등록이 정의되지 않았습니까? 그렇다면 상점 코드를 더 많이 볼 필요가 있습니다. – fisherwebdev

+0

'ShopDispatcher = undefined'를 처음부터 덮어 쓰고 있습니다. ' '고양이 = "고양이"; 그것은 'VAR 고양이의 커피 스크립트에 해당합니다' 어쨌든, 명시 적으로 'jest.mock "../../app/scripts/dispatchers/shop_dispatcher으로 발송자를 조롱. coffee "가 파일 맨 위에있는 테스트를 통과 시켰습니다. ShopDispatcher에 Shop.register.mock 속성이없는 실제 ShopDispatcher를 요구하면이 테스트가 실패하게됩니다. 나는 농담에 더 깊이 파고 더 많은 시험을 쓸 것이다. 도와 줘서 고마워! – johnnyutah

답변

0

나는 또한 같은 문제에 직면했다. ShopDispatcher.register.mock.calls[0][0]을 사용하는 대신 ShopDispatcher.dispatch을 사용해보세요. 아래 코드는 나를 위해 완벽하게 작동합니다 (타입 스크립트 사용).

beforeEach(function() { 
    dispatcher = require("../../../src/App/Dispatcher"); 
    localeStore = require("../../../src/stores/localestore"); 
    localeAction = require("../../../src/actions/Locale/LocaleAction"); 
} 

it("should translate the element with the value in current locale JSON", function() { 
    localeChangeAction = new localeAction(true, locale, localeJson); 
    dispatcher.dispatch(localeChangeAction); 

    var translatedText = localeStore.instance.TranslateText("login.login-header"); 
     expect(translatedText).toEqual("Login header"); 
});