在Rspec测试中扩展FactoryGirl

时间:2013-07-23 15:44:20

标签: rspec factory-bot

我正在尝试在我的rspec测试中使用FactoryGirl函数createbuild,而不是每次都指定FactoryGirl名称空间create而不是FactoryGirl::create

我想我可以通过如下扩展FactoryGirl来做到这一点,但我总是得到NoM​​ethodError。

RSpec.configure do |c|
  c.extend FactoryGirl
end

describe 'my thing' do

  it 'should be ok' do
    obj = create :my_factory
  end

end

我该怎么办?

1 个答案:

答案 0 :(得分:1)

建议的方法如下:

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

“使用工厂”下的https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md中记录了这一点