簡體   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