我在我的Rails应用程序中使用mongoid。因为它不支持灯具,所以我使用factory_girl代替。然后,我建立两个模型:是factory_girl的关联参考或重复产生的?
class User
include Mongoid::Document
field name, type: String
validates :name, uniquess: true
end
class Post
include Mongoid::Document
belongs_to :user
end
然后我建立了两家工厂像belowing:
FactoryGirl.define do
factory :user do
name 'user'
end
end
FactoryGirl.define do
factory :post1, class: 'Post' do
user
end
end
FactoryGirl.define do
factory :post2, class: 'Post' do
user
end
end
就像上面,创建具有两个职位的用户,这是我想要的。
我按照灯具的思想,即在运行测试用例之前准备的所有数据。
Mongoid::default_client.database.drop
FactoryGirl.lint
但是,它会产生一个错误,因为在后厂多次创建用户不闯民宅用户工厂以前的定义:
rake aborted!
FactoryGirl::InvalidFactoryError: The following factories are invalid:
* user -
message:
Validation of User failed.
summary:
The following errors were found: Name is already taken
resolution:
Try persisting the document with valid data or remove the validations. (Mongoid::Errors::Validations)/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/linter.rb:14:in `lint!'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl/linter.rb:4:in `lint!'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.5.0/lib/factory_girl.rb:59:in `lint'
/home/hello/workspace/scaffold/test/test_helper.rb:6:in `<top (required)>'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:31:in `create_and_run_single_test'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/sub_test_task.rb:17:in `invoke_rake_task'
/home/hello/workspace/scaffold/vendor/bundle/ruby/2.1.0/gems/railties-4.2.5/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:single
(See full trace by running task with --trace)
你擦你的数据库? – CallmeSurge
是的。这行做到这一点:Mongoid :: default_client.database.drop – Run