[英]Testing named_scope with Mongoid and rspec
I'm newbie in RoR, and I am trying to test a simple named_scope for my Model. 我是RoR的新手,并且正在尝试为我的模型测试一个简单的named_scope。
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. 但是我不知道我的模型(我正在使用mongoid),代码测试(我正在使用rspec)还是工厂是否存在问题。 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.
Mongoid :: Errors :: InvalidCollection:由于是嵌入式文档,因此不允许访问Movement的集合,请从根文档访问集合。
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_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.
Mongoid :: Errors :: InvalidCollection:由于它是嵌入式文档,因此不允许访问Motion的集合,请从根文档访问集合。
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 #
失败/错误:@movements =(1..3).collect {Factory(:movement)} NoMethodError:#的未定义方法'reflect_on_association'
Could someone help me? 有人可以帮我吗?
Thanks 谢谢
I just had a the same error in my app. 我的应用程式中只有一个相同的错误。 I ended up having an error in my class and that solved my problem.
我最终在班上遇到了一个错误,这解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.