【问题标题】:Unit test (Rspec) cannot update `current_user` in Rails单元测试(Rspec)无法更新 Rails 中的“current_user”
【发布时间】:2018-01-23 13:05:08
【问题描述】:

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

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

而函数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

现在,当我在rspec 上方运行时, current_useruser_detail 得到更新(与binding.pry 核对),但登录用户@group_useruser_detail 没有得到更新。因此我的expect 失败了。

我在测试这个功能时做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby unit-testing rspec


    【解决方案1】:

    您可能需要在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多