I’m testing a controller action that uses includes in the find statement. It raises RecordNotFound when the test runs. Am I missing something? How should I be handling tests on such things?
Controller:
def show
@forum_sub_topic = ForumSubTopic.includes(:forum_posts => [:post_replies]).find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @forum_sub_topic }
end
end
Test:
it 'renders show template' do
ForumSubTopic.stub(:find).with("37") { mock_forum_sub_topic }
get :show, :id => "37"
response.should render_template('show')
ebd
You’re stubbing
:findonForumSubTopic, but your controller is calling.findon anActiveRecord::Relationobject rather than theForumSubTopicmodel.If you don’t care so much about the
with("37")part (because I’m not sure if it’s possible doing this), RSpec provides astub_chainmethod that should work for you:Otherwise, you could put multiple stubs in: