繁体   English   中英

使用Mongoid和rspec测试named_scope

[英]Testing named_scope with Mongoid and rspec

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

但是我不知道我的模型(我正在使用mongoid),代码测试(我正在使用rspec)还是工厂是否存在问题。 我收到这个错误

Mongoid :: Errors :: InvalidCollection:由于是嵌入式文档,因此不允许访问Movement的集合,请从根文档访问集合。

我的模特

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

我的工厂,骗子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

我的测试

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 :: Errors :: InvalidCollection:由于它是嵌入式文档,因此不允许访问Motion的集合,请从根文档访问集合。

我在工厂尝试了一些零钱。

   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)} NoMethodError:#的未定义方法'reflect_on_association'

有人可以帮我吗?

谢谢

我的应用程式中只有一个相同的错误。 我最终在班上遇到了一个错误,这解决了我的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM