2010-11-23 2 views
13

메서드를 호출하는 메서드에서 메서드 중 하나가 특정 매개 변수를 가져 오는 지 여부를 테스트하려고합니다. 예를 들어 아래 코드에서 MyModel은 offset 메서드에 대해 매개 변수 0을 받아야합니다. 불행히도 아래 코드는 작동하지 않습니다. should_receive와 stub_chain을 혼합하는 것은 불가능한 것 같습니다. 어떻게 해결할 수 있을까요? 나는 또한 RSpec에 Google 그룹에 질문을 게시 stub_chain with should_receive

데이비드 (RSpec에의 창조자를

tags = taggable.tag_counts.offset(page-1).limit(per_page).where(*where_clause).order("count DESC") 

업데이트했다 : 나는 RSpec에 2

MyModel.should_receive(:offset).with(0).stub_chain(:tag_counts, :offset, :limit, :order).and_return([]) # does not work! 

코드 내가 테스트를 시도하고 사용하고 있습니다) 대답했습니다 (David에게 감사드립니다) : http://groups.google.com/group/rspec/browse_thread/thread/6b8394836d2390b0?hl=en

답변

8

이것은 제가 지금 내 사양 중 하나에서 수행하고있는 예입니다. 그것은 약간의 손재주가 (여러 줄의 원인),하지만 그것을 작동 : 정말 offset가 0이 설정되어 SearchPaginationModel.tag_counts.offset(0).limit(X).where(X).order(X)에서

SearchPaginationModel.stub(:tag_counts) { SearchPaginationModel } 
SearchPaginationModel.should_receive(:offset).with(0) { SearchPaginationModel } 
SearchPaginationModel.stub_chain(:limit, :where, :order) { [] } 
SearchPaginationModel.stub_chain(:tag_counts, :where, :count).and_return(1) 
SearchPaginationModel.search_tags(:page => "1") 

이 예를 들어 테스트합니다.

관련 문제