2011-03-27 6 views
2

Rails 3.0.5, RSpec 2와 Capybara 0.4.1.2를 사용하고 SessionsController # new action에 대한 컨트롤러 스펙을 작성하려고합니다.Rspec와 Capybara 객체 매칭

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

ActiveRecord :: Base 네임 스페이스는 Capybara Session 클래스와 충돌하는 것으로 보이므로 사용하고 있습니다.

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

RSpec에이이 같은 객체 이해하지 않는 것 : 여기

는 SessionsController입니다. 내 검사 결과가 다음과 같습니다.

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

힌트 :

답변

3

문제는 당신이

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

을 할 경우 모두 이러한 개체는 별도의 object_id을 가지고 당신은 거짓 얻을 것이다.

이 시도 :

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

덕분에,이 작품. 호기심에서 ... 여기서 'be_an' 또는'be_an_instance_of'를 사용하면 차이가 있습니까? – Cimm

+0

아 맞습니다. 'be_an'은'kind_of? '를 검사하고'be_an_instance_of'는'instance_of?'를 검사합니다. 따라서 be_an은 AR :: Base :: Session에서 상속받은 모든 클래스의 인스턴스에도 적용됩니다. 자세한 정보 : http://stackoverflow.com/questions/3893278/ruby-kind-of-vs-instance-of-vs-is-a – Dogbert

+0

멋진 답변입니다! – Cimm