2011-03-19 45 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