2013-06-27 8 views
3

다음은 라우터를 테스트하기 위해 question입니다.재스민과 사론이있는 백본 라우터를 테스트합니다. pushState를 호출 할 수 없습니다.

App.Router = Backbone.Router.extend({ 
    routes:{ 
     "": "index", 
     "help": "help" 
    }, 

    help: function() {/* not really needed */ }, 

    index: function(){ 
     // does something 
    } 
}); 

그리고이 sinon와 자스민을 사용하여 테스트해야 무엇의 apptempted 번역 한 것입니다 : 내 라우터는 정말 간단하다 내가 처음에 탐색하기 때문에

it('triggers the "index" route', function() { 
    var router = new App.Router(); 
    Backbone.history.start(); 
     //Not calling navigate it's a problem 
    router.navigate('help', { 
     trigger : true, replace: true 
    }); 
    var index = sinon.spy(router, 'index'); 

    var spyHasPS = sinon.spy(function(
      data, title, url) { 
     expect(url).toEqual('/'); 
     router.index(); 
    }); 

    var spyNoPS = sinon.spy(function(loc, frag) { 
     expect(frag).toEqual(''); 
     router.index(); 
    }); 

    if (Backbone.history._hasPushState) { 
     pushStateSpy = sinon.stub(window.history, 'pushState', spyHasPS); 
    // window.history.pushState(); 
    } else if (Backbone.history._wantsHashChange) { 
     pushStateSpy = sinon.stub(Backbone.history, '_updateHash', spyNoPS); 
     //Backbone.history._updateHash(window.location, ''); 
    } 

    router.navigate('', { 
     trigger : true, replace: true 
    }); 
    expect(pushStateSpy.called).toBe(true); 
    expect(index.called).toBe(true); 

}); 

이 시험 작품을하지만 난 그것을 달성 할 수 "도움". "도움"은 내가 시험에 합격하기 위해 만든 것이지만 원래의 질문은 그것을하지 않고 통과하고있었습니다. 내가 뭐 잘못 했어요? 나는 또한 그의 테스트를 실행하지만 오류는 다음과 같습니다.

Expected spy _updateHash to have been called. Error: Expected spy 
_updateHash to have been called. 
    at null.<anonymous> (/src/test/js/spec/wfcRouter.spec.js:65:32)  Expected spy index to have been called. 

"문제"가 탐색 기능에 있다고 생각합니다. 그냥 (하나 개의 경로가 난 그냥이 테스트 통과를 만들기 위해 "도움"을 추가 기억하는 경우가 pushState를 테스트하는 의미가 않습니다

fragment = this.getFragment(fragment || ''); 
    if (this.fragment === fragment) return; 

그래서 ... 다음 navigate: function(fragment, options)의 특정 시점에서 우리는이 제어 할 수 있습니다 그래서 나는 그것을 필요로하지 않는다)? 이해가된다면이 시험을 어떻게 할 수 있습니까?

답변

0

테스트중인 것은 백본 코드와 비슷하지만 테스트 할 필요가 없습니다. 아마도 백본 코드가 Jeremy Ashkenas에 의해 충분히 테스트되었습니다 (그리고 GitHub의 Backbone 프로젝트를 보면 사실 그는 포괄적 인 테스트 스위트를 가지고있다). 따라서 코드를 다시 테스트하는 것보다는 이미 테스트를 마친 코드를 작성해야합니다. 실제로 테스트해야 할 코드는 입니다. 당신이 그 원칙에 동의하면

는, 당신은 아래로, 테스트 큰 거래를 단순화 할 수 있습니다 :

it('triggers the "index" route', function() { 
    var router = new App.Router(); 

    router.index(); 
    expect(thingThatShouldHaveHappenedInIndexRouteDidHappen).toBe(true); 
});