rspec expects all :resources to equal resource

I'm new to RSpec, I wonder why I didn't pass this test

before(:each) { get :index }

it "assigns all favorites as @favorites" do
  favorite = FactoryGirl.create(:favorite)
  expect(assigns(:favorites)).to eq([favorite])
end

It says

1) FavoritesController GET index assigns all favorites as @favorites
   Failure/Error: expect(assigns(:favorites)).to eq([favorite])

     expected: [#<Favorite id: 1, patient_id: 6, doctor_id: 5>]
          got: #<ActiveRecord::Relation []>                                           

     (compared using ==)

     Diff:                                                                                    
     @@ -1,2 +1,2 
     -[#<Favorite:0x000000058a5ca0 id: 1, patient_id: 6, doctor_id: 5>]
     +[]

It seems assigns(:favorites) got empty. I have tried another approach as well

def valid_attributes
  doctor = FactoryGirl.create(:doctor)
  patient = FactoryGirl.create(:patient)
  FactoryGirl.attributes_for(:favorite, doctor_id: doctor.id, patient_id: patient.id)
end

it "assigns all favorites as @favorites" do
  favorite = Favorite.create! valid_attributes
  expect(assigns(:favorites)).to eq([favorite])
end

And it got the same error. Any inputs would be helpful for me and I'd like to ask if there is any way to simplify it.

Update

app/controllers/favorite_controller.rb

class FavoritesController < ApplicationController
  before_action :set_favorite, only: [:destroy]
  before_action :authenticate_user!

  def index
    @favorites = Favorite.where(:patient_id => current_user.id).order(id: :asc)
  end
end

spec/controllers/favorite_controller_spec.rb

require 'spec_helper'

describe FavoritesController, type: :controller do

  login_patient

  describe "GET index" do
    let!(:favorite) { FactoryGirl.create(:favorite) }
    before { get :index }

    it { expect(response).to render_template(:index) }
    it { expect(response).to be_success }
    it { expect(response).to have_http_status(200) }

    it "blocks unauthenticated access", :skip_before do
      expect(response).to redirect_to(new_user_session_path)
    end

    it "assigns all favorites as @favorites" do
      expect(assigns(:favorites).to_a).to eq([favorite])
    end
  end
end

spec/support/controller_helpers.rb

module ControllerHelpers
  def login_patient
    before :each do |example|
      unless example.metadata[:skip_before]
        @request.env["devise.mapping"] = Devise.mappings[:user]
        @patient = FactoryGirl.create(:patient)
        sign_in :user, @patient
      end
    end
  end
end

Answers


Try to create the records before the request:

let!(:favorite) { FactoryGirl.create(:favorite) }
before(:each) { get :index }

it "assigns all favorites as @favorites" do
  expect(assigns(:favorites).to_a).to eq([favorite])
end

You are creating the records after you send the request so by the time the request finishes, the record you just created isn't included in the list of favorites. Change your test code to the following

let!(:favorite) { FactoryGirl.create(:favorite) }

before { get :index }

it "assigns all favorites as @favorites" do
  expect(assigns(:favorites)).to eq([favorite])
end

This will probably still fail because assigns(:favorites) is an ActiveRecord::Relation object so you have to call to_a

expect(assigns(:favorites).to_a).to eq([favorite])

UPDATE:

Since the favorite is being filtered by patient, you have to make sure that the created favorite in the test belongs to the patient. You can do that by changing the favorite to

let!(:favorite) { FactoryGirl.create(:favorite, patient: @patient)

Need Your Help

What is the most flexible serialization for .NET objects, yet simple to implement?

c# .net json serialization json.net

I would like to serialize and deserialize objects without having to worry about the entire class graph.

Why do I get an “Invalid handle” error using GetFileSizeEx() with files marked read-only?

windows delphi winapi file handle

When I use the Windows API call GetFileSizeEx() from my Delphi 6 app on a read-only file, I get an O/S error code 6 ("Invalid file handle"). If I remove the read-only attribute from the file, the ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.