使用 Mongoid 和 rspec 测试named_scope

发布于 2024-12-13 04:45:39 字数 1624 浏览 1 评论 0原文

我是 RoR 的新手,我正在尝试为我的模型测试一个简单的named_scope。

但我不知道我的模型(我使用 mongoid)、代码测试(我使用 rspec)或我的工厂是否有问题。我收到这个错误

Mongoid::错误::无效集合: 不允许访问移动集合,因为它是嵌入文档,请从根访问集合 文档。

我的模型

class Movement
    include Mongoid::Document
    field :description, :type => String
    embedded_in :category

    named_scope :top, lambda { |number| { :limit => (number.size > 0 ? number : 10) } }    
end

class Category
  include Mongoid::Document
  field :name
  embeds_many :movement
end

我的工厂,confactory_girl

Factory.define :movement do |m|
  m.amount 24
  m.date "30/10/2011"
  m.description "Beer"
  m.association :category, :factory => :category
end

Factory.define :category do |c|
  c.name "Drink"
end

我的测试

describe "when i have a movement list" do
  it "recent method should return last 2 movements" do
    @movements = (1..3).collect { Factory(:movement) }
    recent_movements = Movement.top(2)
    recent_movements.should have(2).entries
  end
end

和错误:

Mongoid::错误::无效集合: 不允许访问移动集合,因为它是嵌入的 > 文档, 请从根文档访问集合。

我尝试在工厂里做一些改变。

   Factory.define :movement do |m|
      m.amount 24
      m.date "30/10/2011"
      m.description "Beer" 
      m.category { [ Factory.build(:category) ] }
    end

但后来我得到了其他不同的错误:

失败/错误:@movements = (1..3).collect { Factory(:movement) } 无方法错误: # 的未定义方法“reflect_on_association”

有人可以帮助我吗?

谢谢

I'm newbie in RoR, and I am trying to test a simple named_scope for my Model.

But I don't know if I have a problem in my model (I'm using mongoid), in my code test (I'm using rspec) or in my factory. I got this error

Mongoid::Errors::InvalidCollection:
Access to the collection for Movement is not allowed since it is an embedded document, please access a collection from the root
document.

My models

class Movement
    include Mongoid::Document
    field :description, :type => String
    embedded_in :category

    named_scope :top, lambda { |number| { :limit => (number.size > 0 ? number : 10) } }    
end

class Category
  include Mongoid::Document
  field :name
  embeds_many :movement
end

My factory, con factory_girl

Factory.define :movement do |m|
  m.amount 24
  m.date "30/10/2011"
  m.description "Beer"
  m.association :category, :factory => :category
end

Factory.define :category do |c|
  c.name "Drink"
end

My test

describe "when i have a movement list" do
  it "recent method should return last 2 movements" do
    @movements = (1..3).collect { Factory(:movement) }
    recent_movements = Movement.top(2)
    recent_movements.should have(2).entries
  end
end

And the error:

Mongoid::Errors::InvalidCollection:
Access to the collection for Movement is not allowed since it is an embedded >document,
please access a collection from the root document.

I tried a little change in my factory.

   Factory.define :movement do |m|
      m.amount 24
      m.date "30/10/2011"
      m.description "Beer" 
      m.category { [ Factory.build(:category) ] }
    end

But then I got other different error:

Failure/Error: @movements = (1..3).collect { Factory(:movement) }
NoMethodError:
undefined method `reflect_on_association' for #

Could someone help me?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-12-20 04:45:39

我的应用程序中刚刚出现了同样的错误。我最终在课堂上犯了一个错误,这解决了我的问题。

I just had a the same error in my app. I ended up having an error in my class and that solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文