Using FactoryGirl without Rails, ActiveRecord or any database with RSpec

馋奶兔 提交于 2019-12-05 08:00:27

Your surmise is correct -- you still have to write the User class yourself. factory_girl works fine with non-ActiveRecord classes, but it doesn't write them for you.

By default factory_girl constructs instances by calling new with no parameters, so your class should either not def initialize or initialize must take no parameters, just like an ActiveRecord model. So the easiest thing to do would be to write User like so:

class User
  attr_accessor :firstname, :lastname, :age
end

If you decide you need an initialize that requires parameters, you can use factory_girl's initialize_with to tell it how to instantiate your class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!