为什么不设计相关的规范工作?

时间:2011-03-19 03:24:54

标签: 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
    @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,但似乎任何属性更改都不会发生在规范中的@user(:user是我创建的工厂)。

为什么这不起作用? :/

1 个答案:

答案 0 :(得分:1)

首先,在发布到@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