Я довольно новичок в использовании rspec и пытаюсь написать свои тесты для моих контроллеров. У меня есть этот контроллер (я использую мокко для заглушки):
class CardsController < ApplicationController
before_filter :require_user
def show
@cardset = current_user.cardsets.find_by_id(params[:cardset_id])
if @cardset.nil?
flash[:notice] = "That card doesn't exist. Try again."
redirect_to(cardsets_path)
else
@card = @cardset.cards.find_by_id(params[:id])
end
end
end
Я пытаюсь проверить это действие примерно так:
describe CardsController, "for a logged in user" do
before(:each) do
@cardset = Factory(:cardset)
profile = @cardset.profile
controller.stub!(:current_user).and_return(profile)
end
context "and created card" do
before(:each) do
@card = Factory(:card)
end
context "with get to show" do
before(:each) do
get :show, :cardset_id => @cardset.id, :id => @card.id
end
context "with valid cardset" do
before(:each) do
Cardset.any_instance.stubs(:find).returns(@cardset)
end
it "should assign card" do
assigns[:card].should_not be_nil
end
it "should assign cardset" do
assigns[:cardset].should_not be_nil
end
end
end
end
end
Пройдены тестовые испытания "следует назначить карточку", но я не могу понять, как правильно заглушить эту строку @card = @cardset.cards.find_by_id(params[:id])
для теста "следует назначить карточку". Как лучше всего протестировать это действие, или, если я на правильном пути, как бы правильно заглушить мои вызовы модели?