我尝试像这样使用 rspec 测试 rails 控制器:
require 'spec_helper'
describe NewsController do
describe "GET 'edit'" do
before(:each) do
@news_1 = FactoryGirl.create(:news_1)
get :edit, { :id => @news_1.id }
end
it { response.should be_success }
it { assigns(:news).should eq(@news_1) }
it { response.should render_template(:edit) }
end
end
但是,我得到了这个错误。
Failures:
1) NewsController GET 'edit'
Failure/Error: it { assigns(:news).should eq(@news_1) }
expected: #<News id: 1, title: "news_title_1", contents: "news_contents_1", display: nil, created_at: "2012-11-29 07:24:49", updated_at: "2012-11-29 07:24:49", news_date: nil, orion: false, pw: false, op: false, pickup: false, info_1: nil, info_2: nil, info_3: nil, info_4: nil, info_5: nil, del: false, place: nil, contact: false>
got: #<News id: 1, title: "news_title_1", contents: "news_contents_1", display: nil, created_at: "2012-11-29 07:24:49", updated_at: "2012-11-29 07:24:49", news_date: nil, orion: false, pw: false, op: false, pickup: false, info_1: nil, info_2: nil, info_3: nil, info_4: nil, info_5: nil, del: false, place: nil, contact: false>
(compared using ==)
Diff:#<News:0x000001030b55c8>.==(#<News:0x000001033126b8>) returned false even though the diff between #<News:0x000001030b55c8> and #<News:0x000001033126b8> is empty. Check the implementation of #<News:0x000001030b55c8>.==.
# ./spec/controllers/news_controller_spec.rb:73:in `block (3 levels) in <top (required)>'
我认为这些值是相同的,但对象 id 是不同的。所以这个测试失败了......我该如何解决这个错误?