Ruby on rails 为什么';设计相关的规范工作?

Ruby on rails 为什么';设计相关的规范工作?,ruby-on-rails,rspec,Ruby On Rails,Rspec,首先,让我说登录是正确的。用户确实已登录。我还确信帖子的发布是正确的(检查了消息和刷新,所以我确定)。正如测试所描述的那样,递增的实际操作效果很好。只有测试失败 但在以下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

首先,让我说登录是正确的。用户确实已登录。我还确信帖子的发布是正确的(检查了消息和刷新,所以我确定)。正如测试所描述的那样,递增的实际操作效果很好。只有测试失败

但在以下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_点应该失败。我实际上在我的宏中使用了一个login_用户函数(如Desive中所述),比如:

我确信@user确实是当前的_用户,但在规范中,@user似乎没有发生任何属性更改(:user是我创建的工厂)


为什么这不起作用/

首先,在发布到
:train_ability
之前,您没有保存
@用户。
在这之后,
@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
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