单元测试(Rspec)无法在Rails中更新`current_user`

Dar*_*pan 0 ruby unit-testing rspec ruby-on-rails

我有这种类型的单元测试 -

describe "#test_feature" do
  it "should test this feature" do
    sign_in @group_user
    get :get_users, {name: "darpan"}
    expect(@group_user.user_detail.name).to eq("darpan")
    sign_out @group_user
  end
end
Run Code Online (Sandbox Code Playgroud)

和功能get_users是这样的 -

def get_users
  quick_start = current_user.user_detail.quick_start
  current_user.user_detail.update_attribute(:name, "darpan")
  render :json => :ok
end
Run Code Online (Sandbox Code Playgroud)

现在,当我在上面运行rspec, current_useruser_detail被更新(有检查binding.pry),但登录用户@group_useruser_detail未更新.因此我expect失败了.

我在测试这个函数时做错了什么?

mov*_*son 5

您可能需要在RSpec中重新加载资源才能看到更改:

describe "#test_feature" do
  it "should test this feature" do
    sign_in @group_user
    get :get_users, {name: "darpan"}
    @group_user.reload   # Reloads information from the database
    expect(@group_user.user_detail.name).to eq("darpan")
    sign_out @group_user
  end
end
Run Code Online (Sandbox Code Playgroud)