我是RoR中的新手,我試圖爲我的模型測試一個簡單的named_scope。使用Mongoid和rspec測試named_scope
但我不知道我的模型(我使用mongoid)是否有問題,在我的代碼測試中(我使用rspec)還是在我的工廠中。我得到這個錯誤
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.
我的模型
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
我廠,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
我的測試
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: Access to the collection for Movement is not allowed since it is an embedded >document, please access a collection from the root document.
我在我的工廠嘗試了一點變化。
Factory.define :movement do |m|
m.amount 24
m.date "30/10/2011"
m.description "Beer"
m.category { [ Factory.build(:category) ] }
end
但後來我得到了不同的錯誤:
Failure/Error: @movements = (1..3).collect { Factory(:movement) } NoMethodError: undefined method `reflect_on_association' for #
有人能幫助我嗎?
謝謝