Convert fixtures into Factory Girl in Rails

2019-02-14 06:55发布

I'd like to migrate my fixtures to "Factory Girl" in Rails.

Is there any easy way to convert all yml files in a factories.rb file?

3条回答
Bombasti
2楼-- · 2019-02-14 07:43

I've just released a gem called fix_to_chix a few days ago to address the exact same issue.

It probably needs a lot of enhancements, but it might help you: http://github.com/caike/fix_to_chix (you need to copy and paste it on your browser since SO seems to put some chars in the url)

查看更多
beautiful°
3楼-- · 2019-02-14 07:50

What exactly would you expect the resulting factories to be? The whole idea of Factories is that you don't have to maintain a long list of test data. Instead, the data is created on the fly, close to your tests, where it should be.

查看更多
Bombasti
4楼-- · 2019-02-14 07:58

I am assuming what you are looking to do is find a script which will look through your models and generate the factories for them. I tried this once (with something other than factory_girl) and found it full of bad data.

I would suggest that you slowly transition to using factories. As you write new tests or update old ones, create the necessary factories. As time goes by, if you have the time and energy you can then choose an individual test case and replace all the fixtured data calls with the proper factory calls and remove the specific fixture requirement at the top of the test case.

查看更多
登录 后发表回答