开发者

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 C开发者_StackOverflow中文版apybara 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜