使用 Rails 3.0.5、RSpec 2 和 Capybara 0.4.1.2,我正在尝试为我的 SessionsController#new 操作编写 Controller 规范。
it "assigns the empty session to a variable" do
get :new
assigns(:session).should == ActiveRecord::Base::Session.new
end
我正在使用 ActiveRecord::Base 命名空间,因为当我不使用时,它似乎与 Capybara Session 类发生冲突。
这是 SessionsController:
class SessionsController < ApplicationController
def new
@session = Session.new
end
end
RSpec 似乎不明白这些是相同的对象。这是我的测试返回的结果:
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)>'
有什么提示吗?
最佳答案
问题是如果你这样做
ActiveRecord::Base::Session.new == ActiveRecord::Base::Session.new
您会得到 false,因为这两个对象都有单独的 object_id
。
试试这个:
assigns(:session).should be_an(ActiveRecord::Base::Session)
关于ruby-on-rails-3 - Rspec 和 Capybara 对象匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5448588/