i have a @user instance that i use for logging a devise user. It's a macro as Devise wiki proposes :
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = :user
@user = Factory(:user)
sign_in @user
end
end
end
Generally, i do different things to stub methods, but i don't understand why this stub does not work : (note that @user is indeed logged in, i use it successfully for testing attributes and stuff)
@user.stub(:has_tavern_quest?).and_return(true)
It seems that the stub works(i checked with @user.has_tavern_quest should true), but i just get back :
Failure/Error: flash[:error].should == I18n.t('error.has_quest')
expected: "You already have a quest to finish !"
got: nil (using ==)
The whole controller method is :
quest_type = params[:quest_type]
@quest_minutes = TavernQuest.get_quest_minutes_from_quest_type(quest_type)
flash[:error] = I18n.t('error.invalid_post') and redirect_to tavern_url and return unless [1,2,3,4].include? quest_type.to_i
flash[:error] = I18n.t('error.has_quest') and redirect_to tavern_url and return if current_user.has_tavern_quest?
flash[:error] = I18n.t('error.no_adv_points') and redirect_to tavern_url and return if current_user.adventure_points < @quest_minutes
current_user.reduce_adventure_points(@quest_minutes.to_i)
TavernQuest.create(:user_id => current_user.id, :start_time => Time.now, :end_time => Time.now + @quest_minutes.minutes,
:duration => @quest_minutes, :quest_type => quest_type)
redirect_to tavern_url
end
And the whole spec is :
it "should redirect to '/tavern' with an error if user already has a tavern quest"开发者_开发百科 do
@user.stub(:has_tavern_quest?).and_return(true)
post :create, :quest_type => 3
flash[:error].should == I18n.t('error.has_quest')
response.should redirect_to tavern_url
end
Anybody knows why and what is the way to make it work ?
Solved :
def login_user
before(:each) do
@user = Factory(:user)
controller.stub(:current_user) { @user }
sign_in @user
end
end
精彩评论