2011-04-20 3 views
1

특정 컨트롤러 작업에서 인스턴스 메서드가 한 번 호출되었다는 것을 주장하려고합니다. 어떤 이유로 든 테스트가 작동하지 않습니다. 코드는 실제로 그것이 한 일을 실제로합니다. 내가 RSpec에 1.x 및 factory_girl컨트롤러에서 인스턴스 메서드가 한 번 호출되었다고 가정하십시오.

테스트

context 'as a regular API user' do 
    before do 
    login_as_regular_user 
    Feed.superfeedr_api.stub!(:pingable?).and_return(true) 
    @feed_entry = Factory(:feed_entry) 
    post :read, :id => @feed_entry.id, :format => 'json' 
    end 

    it "should call read_by method once" do 
    @user = Factory.create(:user) 
    controller.stub!(:current_account).and_return(@user.account) 

    @feed_entry.should_receive(:read_by).once 
    post :read, :id => @feed_entry.id, :format => 'json'   
    end 

    it { should respond_with(204)} 
    it { should assign_to(:feed_entry).with(@feed_entry) } 
    it { should respond_with_content_type(/json/) }    
end 

컨트롤러

# POST /entries/1234/read - mark as read 
# DELETE /entries/1234/read - mark as unread 
def read 
    if request.post?  
    @feed_entry.read_by(current_account) 
    elsif request.delete? 
    @feed_entry.unread_by(current_account) 
    end  

    respond_to do |format| 
    format.html { redirect_to topic_path(params[:topic_id]) } 
    format.json { render :nothing => :true, :status => :no_content } 
    format.plist { render :nothing => :true, :status => :no_content } 
    end 
end 

오류

1) 
Spec::Mocks::MockExpectationError in 'FeedEntriesController.read as a regular API user should call read_by method once' 
#<FeedEntry:0x107db8758> expected :read_by with (any args) once, but received it 0 times 
./spec/controllers/feed_entries_controller_spec.rb:82: 

Finished in 2.242711 seconds 

25 examples, 1 failure, 3 pending 

답변

2

을 사용하고

난 당신이 (일부 코드가 같은데요 쇼가 아닌 ID로 FeedEntry을 발견하고 그것을 할당하는 예 WN)는 @feed_entry하기 : 찾기 방법은 당신의 팩토리 인스턴스를 반환하도록 스텁해야

# controller 
@feed_entry = FeedEntry.find(params[:id]) 

가 :

# spec 
FeedEntry.should_receive(:find).and_return(@feed_entry) 

이 될 수 테스트중인 코드와 스펙에서 인스턴스 변수 이름이 같은 이름을 가질 때 혼란 스럽습니다. 다른 객체라는 것을 잊지 마십시오.

+0

브릴리언트, 어떻게 든이 명백한 문제를 보지 못했습니다. 고맙습니다. – brupm

관련 문제