2011-04-22 2 views
3

저는 inherited_resources 컨트롤러에 대한 스펙을 작성하려고합니다. rspec의 mock_model을 사용하여 데이터베이스와의 모든 통합을 모의하기로 결정했습니다. 불행히도 다음과 같은 오류가 발생하기 때문에 작성 및 업데이트 작업에 대한 스펙을 작성할 수 없습니다. https://gist.github.com/936947 누군가이 문제를 해결할 수 있습니까?RSpec mock_model 및 inherited_resources

답변

4

나는 flexmock을 사용하여 동일한 문제를 겪고있었습니다.

원인은 라우팅 결정을 위해 update_attributes 메서드를 사용하지 않기 때문입니다. resource.errors이 비어 있는지 확인합니다.

올바르게 응답하려면 errors 메서드를 조롱해야합니다.

은 여기 아무 문제없이 작동 실제 모델 lib 디렉토리/inherited_resources/base_helpers.rb

def respond_with_dual_blocks(object, options, &block) #:nodoc: 
    args = (with_chain(object) << options) 

    case block.try(:arity) 
     when 2 
     respond_with(*args) do |responder| 
      blank_slate = InheritedResources::BlankSlate.new 
      if object.errors.empty? 
      block.call(responder, blank_slate) 
      else 
      block.call(blank_slate, responder) 
      end 
     end 
     when 1 
     respond_with(*args, &block) 
     else 
     options[:location] = block.call if block 
     respond_with(*args) 
    end 
    end 
+0

내 문제가 해결되었습니다. 고마워요! ;) – luacassus

+0

정확히 어떻게 오류 방법을 모의? 이 똑같은 문제를 겪었습니다 ... – sevenseacat

+1

아, 저는 team.stub_chain (: errors, : empty?) and_return (false)와 stub_chain을 사용했습니다. 완벽하게 작동합니다. – sevenseacat

0

실패 메시지는 컨트롤러 내부에서 명명 된 경로에 액세스 할 수 없다는 것에 대한 것이므로 mock_model과 관련이 있는지 확신 할 수 없습니다. 실제 모델을 사용하여 동일한 예제를 사용해 보셨습니까?

+0

에서 관련 코드 @line 248입니다. – luacassus