Rails / AR - 如何在rspec中测试:: update(id,attributes)

时间:2015-10-06 12:11:39

标签: ruby-on-rails activerecord rspec

我正在使用活动记录update方法更新多条记录,每条记录都有各自的属性。

我使用这个控制器代码(有效)来促进这一点:

def update
  keys = params[:schedules].keys
  values = params[:schedules].values
  if Schedule.update(keys, values)
    flash[:notice] = "Schedules were successfully updated."
  else
    flash[:error] = "Unable to update some schedules."
  end
  respond_to do |format|
    format.html { redirect_to responsibilities_path }
  end
end

我的问题是,如何在没有点击rspec中的数据库的情况下测试

这是我正在尝试的,但它不起作用。

describe "PATCH update" do

  it "updates the passed in responsibilities" do
    allow(Schedule)
      .to receive(:update)
      .with(["1", "2"], [{"status"=>"2"}, {"status"=>"1"}])
      .and_return(true)
    # results in
    # expected: 1 time with arguments: (["1", "2"], [{"status"=>"2"}, {"status"=>"1"}])
    # received: 0 times
    # Couldn't find Schedule with 'id'=1

    # without the allow, I get
    # Failure/Error: patch :update, schedules: {
    # ActiveRecord::RecordNotFound:
    #   Couldn't find Schedule with 'id'=1
    # # ./app/controllers/responsibilities_controller.rb:18:in `update'
    # # ./lib/authenticated_system.rb:75:in `catch_unauthorized'
    # # ./spec/controllers/responsibilities_controller_spec.rb:59:in `block (5 levels) in <top (required)>'

    patch :update, schedules: {
      "1" => {
        "status" => "2",
      },
      "2" => {
        "status" => "1",
      }
    }
    expect(Schedule)
      .to receive(:update)
      .with(["1", "2"], [{"status"=>"2"}, {"status"=>"1"}])
    expect(flash[:error]).to eq(nil)
    expect(flash[:notice]).not_to eq(nil)
  end
end

我使用的是Rails 4.2.4和rspec 3.0.0

1 个答案:

答案 0 :(得分:2)

你的问题是,你期待

expect(Schedule)
      .to receive(:update)
      .with(["1", "2"], [{"status"=>"2"}, {"status"=>"1"}])
      .and_call_original     

调用补丁方法后。

这意味着请求在断言建立之前命中您的控制器。 要解决这个问题,只需在补丁调用之前调用expect(Schedule)调用,这也可以让你摆脱allow(Schedule).to - call。

干杯。