2010-12-08 5 views
0

환경은 Sinatra, Nokogiri, RSpec, FactoryGirl 및 DataMapper입니다.RSpec & DataMapper : 파인더 메소드를 올바르게 스터브하는 방법

문제는 DataMapper의 serializer에서 발생합니다. 나는 그것으로 적절히 스터브하는 법을 모른다.

문제가있는 코드 :

specify 'should return an xml array of all municipalities' do 
    municipalities = [] 
    10.times { |n| municipalities << Factory.build(:municipality, :code => "Municipality no #{n}") } 
    Municipality.stub!(:all).and_return(municipalities) 
    get "/municipalities.xml" 
# ^------- KABOOM! 
# [.. rest of code clipped out ..] 
end 

코드 get 통화와 실질적으로 동일하다 :

Municipality.all.to_xml 

문제는 결과 집합의 유형이 #to_xml 방법을 가지고 DataMapper::Collection 것입니다.

DataMapper::Collection 대신에 Array의 스터브 된 인스턴스를 사용하기 때문에 내 스텁이 작동하지 않는 것 같습니다.

내 질문은 :

어떻게 유형 DataMapper::Collection의이고 DataMapper의 시리얼에서 제공하는 #to_xml 방법이 (FactoryGirl와) 인스턴스의 컬렉션을 만들어야합니다 ?

코드

는 Github에서에서로도 주문 가능합니다

답변

1

글쎄, 당신은하지 않습니다.

우리가 알고 있기 때문에 :

  • .all 항상 반환하는 DataMapper::Collection 그것은 다른 단위 테스트에서 테스트해야
  • .to_xml 항상 수집의 XML 표현을 반환합니다
    • 그것은 diffenent 단위로 HTTP GET 등

그래서 우리는 하나 개의 테스트에서 모든 테스트를 중지하고 그것을 나눌 수와 아무 상관이있다 테스트를 테스트해야합니다. 컨트롤러가 Municipality.all을 요구하고 위의 코드는 어쩌면이다 "/municipalities.xml"

specify 'should return an xml array of all municipalities' do 
    collection = mock(DataMapper::Collection (or anything, its not really interesting here)) 
    collection.should_receive(:to_xml).and_return(xml_if_needed_for_view) 

    Municipality.should_receive(:all).and_return(collection) 

    get "/municipalities.xml" 
end 

의 GET과의 호출을 얻을 때 다음 .to_xml 컬렉션을 위해 그것을 돌려주는 경우

당신이 정말로 여기 테스트 할 것은 다소 의사.

+0

내 비전을 넓혀 주셔서 감사합니다! :) –