ruby-on-rails - 太阳黑子的奇怪问题。

标签 ruby-on-rails testing activerecord rspec sunspot

我有模型:

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">

我做错了什么?这个错误出现在我开始使用 sunspot 之后。

在我的 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

所有测试成功通过!

最佳答案

问题是您正在使用 == 比较数组 (@topic.comments) 和对象 (@comment)。您应该检查该对象是否包含在数组中:

@topic.comments.should include(@comment)

关于ruby-on-rails - 太阳黑子的奇怪问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7105642/

相关文章:

ruby-on-rails - 使用 ActiveRecord 在表上自连接

ruby-on-rails - 如何在不实际执行的情况下获取由 ActiveRecord#find 创建的 SQL 语句?

ruby-on-rails - 你如何在 rails 3.1 中删除 `references` 列?

ruby-on-rails - 如何从 Rails 自动加载中删除文件夹?

ruby-on-rails - 如何从 Rails 中的 Controller 中过滤关联记录?

iphone - 多个操作系统版本上的 iPhone 应用程序的最佳实践

C# 模拟系统崩溃

ruby-on-rails - `/home/webapp` 不是目录 - Elastic Beanstalk (RAILS)

angularjs - 测试中的 Angular 1.x 模拟依赖项

ruby-on-rails - ActiveResource 模型可以与 ActiveRecord 模型集成吗?