我有模特:
class Topic < ActiveRecord::Base
attr_accessible :title, :description
has_many :comments, :dependent => :destroy
end
和
class Comment < ActiveRecord::Base
attr_accessible :body, :topic_id
belongs_to :topic
end
在我的topic_spec.rb
:
it "should have the right associated comment" do
@topic.comments.should include(@comment)
end
it "should destroy associated comments" do
@topic.destroy
Comment.find_by_id(@comment.id).should be_nil
end
我收到以下错误:
1) Failure/Error: @topic.comments.should == @comment
NameError:
undefined method `inspect' for class `ActiveRecord::Associations::CollectionProxy'
2) Failure/Error: Comment.find_by_id(@comment.id).should be_nil
expected: nil
got: #<Comment id: 1, body: "first", created_at: "2011-08-18 09:55:06", updated_at: "2011-08-18 09:55:06">
我做错了什么?我开始使用太阳黑子后出现了这个错误。
在我的topic.rb中:
searchable :auto_index => true, :auto_remove => true do
text :title, :boost => 5
text :description
text :comments do
comments.map(&:body)
end
end
如果我评论这一行:
# text :comments do
# comments.map(&:body)
# end
所有测试成功通过!
答案 0 :(得分:1)
问题是您要将数组(@topic.comments
)和对象(@comment
)与==进行比较。您应该检查对象是否包含在数组中:
@topic.comments.should include(@comment)
答案 1 :(得分:0)
我通过在spec_helper.rb
中添加以下行来解决了这个问题:
config.before(:each) do
::Sunspot.session = ::Sunspot::Rails::StubSessionProxy.new(::Sunspot.session)
end
config.after(:each) do
::Sunspot.session = ::Sunspot.session.original_session
end