2011-03-19 43 views
0

首先,讓我說,登錄工作正常。用戶登錄確定。我也確定帖子發生的正確(檢查郵件和刷新,所以我確定)。正如測試所描述的那樣,遞增的實際行動很好。只有測試失敗。爲什麼不制定相關的規範工作?

但低於這個RSpec的:

it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do 
    @user.str = 10 
    @user.str_points = 0 
    post :train_ability, :ability => 'str' 
    flash[:error].should be_nil 
    @user.str_points.should == 1 
    @user.str.should == 11 
end 

的STR和str_points不宜失敗。實際上,我在我的宏使用login_user功能(如色器件規定),如:

module ControllerMacros 
    def login_user 
    before(:each) do 
     @request.env["devise.mapping"] = :user 
     @user = Factory.create(:user) 
     sign_in @user 
    end 
    end 
end 

我敢肯定,@user的確是CURRENT_USER,但似乎任何屬性的變化不會真的發生到spec中的@user(:user是我創建的工廠)。

爲什麼不能正常工作? :/

回答

1

首先,您在發佈到:train_ability之前沒有保存@user。 你的@user也可能會被緩存,所以在你的斷言可能是必要的之前重新加載它。

試着改變你的規格以下

it "should increase the strength ability by one point and also update the strength_points by one if strength is the trained ability" do 
    @user.str = 10 
    @user.str_points = 0 
    @user.save! # save the @user object so str is 10 and str_points are 0 
    post :train_ability, :ability => 'str' 
    flash[:error].should be_nil 
    @user.reload # reload the user in case str and str_points are cached 
    @user.str_points.should == 1 
    @user.str.should == 11 
end 
+0

這兩點是很重要的! thanx,現在工作! – Spyros 2011-03-19 19:26:08