Question

The second test here passes fine, the first doesn't. No idea why.

require 'spec_helper'

RSpec.describe UserNotesController do

  context 'GET #index' do

    let(:note) { build(:user_note) }

    specify 'populates an array of notes' do
      expect(:user_notes).to eq [note]
    end

    specify 'renders the :index view' do
      get :index
      expect(response).to render_template :index
    end
  end
end

Not sure why it's not passing but I'm getting this message.

(compared using ==)

Diff:
@@ -1,2 +1,2 @@
-[#<UserNote id: nil, user_id: 2, creator_id: nil, note: "Magnam quas fugit nihil.", created_at: nil, updated_at: nil, follow_up: nil>]
+:user_notes

I'm pretty new to Rspec. Any help?

Était-ce utile?

La solution

To access an instance variable @foo in an rspect test you reference it with assigns(:foo)

expect(assigns(:user_notes)).to eq [note]

You're also missing the call to the action. It should be...

get :index
expect(assigns(:user_notes)).to eq [note]

Lastly, the let(:note) { build(:user_note) } is lazy and doesn't execute until the variable is referenced... which is only AFTER the get :index, and build won't save to the database in any case, so change it to ...

let!(:note) { create(:user_note) }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top