Cimm
Cimm

Reputation: 4775

Rspec and Capybara object matching

Using Rails 3.0.5, RSpec 2 and Capybara 0.4.1.2 and I'am trying to write a controller spec for my SessionsController#new action.

it "assigns the empty session to a variable" do
  get :new
  assigns(:session).should == ActiveRecord::Base::Session.new
end

I'm using the ActiveRecord::Base namespace as it seems to clash with the Capybara Session class when I don't.

Here is the SessionsController:

class SessionsController < ApplicationController
  def new
     @session = Session.new
  end
end

RSpec doesn't seem to understand these are the same objects. Here is what my test returns:

 Failure/Error: assigns(:session).should == ActiveRecord::Base::Session.new
   expected: #<Session id: nil, session_id: nil, data: nil, created_at: nil, updated_at: nil>
        got: #<Session id: nil, session_id: nil, data: nil, created_at: nil, updated_at: nil> (using ==)
   Diff:
 # ./spec/controllers/sessions_controller_spec.rb:17:in `block (3 levels) in <top (required)>'

Any hints?

Upvotes: 2

Views: 767

Answers (1)

Dogbert
Dogbert

Reputation: 222108

The problem is that if you do

ActiveRecord::Base::Session.new == ActiveRecord::Base::Session.new

You would get false, as both these objects have a separate object_id.

Try this:

assigns(:session).should be_an(ActiveRecord::Base::Session)

Upvotes: 3

Related Questions