问题
I would like to create a factory girl for a model in my server which is inside a folder in the models folder.
My tree view looks like:
├── app
| ├── models
│ │ ├── xxx
│ │ | ├── user.rb
├── spec
│ ├── factories
│ │ ├── xxx
│ │ | ├── user.rb
My factory girl looks like:
FactoryGirl.define do
factory :user do
username { 'aaa' }
end
end
When I try to build user I get the error:
undefined method `new' for User:Module
回答1:
Is your model defined under a namespace? For example, if your app/models/xxx/user.rb
defines a class:
class XXX::User
#...
end
Then in your factory you can do:
FactoryGirl.define do
factory :user, class: XXX::User do
username { 'aaa' }
end
end
来源:https://stackoverflow.com/questions/28265122/how-to-define-factory-girl-in-rails-for-model-that-isnt-in-root-of-the-models-f