Rails Testing Antipatterns: Fixtures and Factories

If your app has at least some tests, congratulations: you’re among the better developers out there. If you think that writing tests is hard — it is, but you just need a little more practice. I recommend reading Rails Testing Handbook and Effective Testing with RSpec 3 if you haven’t already.

If you don’t know how to add more tests to a large system you inherited, I recommend going through Working Effectively with Legacy Code. If you have no one else to talk to about testing in your company, there are many great people to meet at events such as CITCON.

If you recognize some of the practices discussed here in your own code, don’t worry. The methodology is evolving, and many of us have “been there and done that”. And finally, this is all just advice. If you disagree, feel free to share your thoughts in the comment section below. Now, onwards with the code.

Imported from Disqus

Thiago Konrada year ago

Hey there. Awesome post! Thank you!

Where should we put Factory Bot helper methods in a rails project? (like the one you created for orders).
Is it possible to use the same helper for both Rspec tests and seeds.rb?

Thanks,
Thiago

Marko Anastasov Thiago Konrada year ago

Thanks Thiago, glad it was helpful!

If you decide to make helper methods available in more than file, then you can put them in a regular Ruby module and include them wherever accordingly.