pbattisson
pbattisson

Reputation: 1240

rspec :show action test not working with rails

I am testing the show action of my rails controller using the following rspec code:

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

However when running this the test is coming back saying:

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 ==)

The relevant controller code is:

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

As you can see - all fairly standard and boilerplate. Any ideas?

Upvotes: 0

Views: 259

Answers (1)

infused
infused

Reputation: 24337

The problem is that you are assigning @external_datum, but testing for @external_data. Change the expectation to read:

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

Upvotes: 1

Related Questions