2
我有一個@user實例,用於記錄設計用戶。這是一個宏觀的設計維基建議:爲什麼設計用戶實例不會被截斷?
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = :user
@user = Factory(:user)
sign_in @user
end
end
end
一般情況下,我做不同的事情,存根方法,但我不明白爲什麼這個存根不起作用:(注意@user的確登錄了,我用它成功地測試屬性和東西)
@user.stub(:has_tavern_quest?).and_return(true)
看來,存根作品(我與@ user.has_tavern_quest應真)檢查,但我只是拿回:
Failure/Error: flash[:error].should == I18n.t('error.has_quest')
expected: "You already have a quest to finish !"
got: nil (using ==)
全控制器的方法是:
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
末
而且整個規範是:
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
任何人都知道爲什麼,什麼是使它的工作方式?