rspec:show action test不使用rails

时间:2014-08-19 15:56:48

标签: ruby-on-rails ruby rspec

我正在使用以下rspec代码测试我的rails控制器的show动作:

it "assigns @external_data correctly with data stored" do
  sign_in
  ext_data = ExternalDatum.create!(name: 'Test File')
  get :show, id: ext_data.id
  expect(assigns(:external_data)).to eq(ext_data)
end

但是在运行时,测试会回来说:

Failure/Error: expect(assigns(:external_data)).to eq(ext_data)

   expected: #<ExternalDatum id: 33, name: "Test File", url: nil, created_at: "2014-08-19 15:49:13", updated_at: "2014-08-19 15:49:13", description: nil>
        got: nil

   (compared using ==)

相关的控制器代码是:

class ExternalDataController < ApplicationController
  before_action :set_external_datum, only: [:show, :edit, :update, :destroy]

  def show
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_external_datum
      @external_datum = ExternalDatum.find(params[:id])
    end
end

如您所见 - 所有相当标准和样板。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是您要分配@external_datum,但正在测试@external_data。将期望值更改为:

expect(assigns(:external_datum)).to eq(ext_data)