#158 Factories not Fixtures (revised)
Fixtures are external dependencies which can make tests brittle and difficult to read. In this episode I show how to use Factory Girl to generate the needed records directly in the tests.
- source codeProject Files in Zip (88.7 KB)
- mp4Full Size H.264 Video (21.1 MB)
- m4vSmaller H.264 Video (11 MB)
- webmFull Size VP8 Video (13.2 MB)
- ogvFull Size Theora Video (25.2 MB)
I still feel like testing is such a drag, How do I believe?
Luke, build an app then come back after a year and try to make some changes. You'll wish you had written some tests.
I know that sounds like a cop out but I had to force myself to just continue to test. Don't feel like you have to hit that holy grail of 100% coverage or even 50%, just keep testing as much as you know how to. The more you do it, the more you'll eventually get to the point that you realize you can code as fast, if not faster through tests and when you realize that, its an exciting thing!
Even just a few tests are better than no tests at all, so start off with the easiest ones to write that will get you the most "bang for your buck".
I find these are generally the high-level request specs like I show in episode 275. Writing request specs is nearly as easy as walking through the app manually, and once it's done you won't have to manually test it next time.
I have only just found my love for testing. test unit suits me much better, I tried using rspec for years but we never gelled. Maybe try switching test suites?
Thanks for this episode, Ryan!
I have only one question: using two (or more)
itis o'key or it's just an example?
This is a controversial subject, but I don't subscribe to the "one assertion per test" way of thinking. It can really slow down tests if you're not sharing data between assertions, especially in high-level request specs. RSpec 2 shows you exactly what line failed the assertion so it is easy to spot in a multi-assert clause.
Take a look at faker for generating unique object data.
I love using factories, but for some reason I got frustrated with the factory_girl syntax on a recent project and ended up justing creating my own factories.rb file in the support/ directory that didn't use an external gem.
It is surprisingly easy to do in plain old active record with a familiar syntax, so now I'm not sure it even warrants a separate gem. May just be personal preference though.
Thanks for the great episode, but erg... I'm a little frustrated with the RailsCasts site.
I wrote a comment then cmd + clicked "Show Notes" to try to reference something in the show notes thinking it would pop open that page in a new browser window. Unfortunately, it didn't. When I tried to click back to get back to this page, I had lost my written-but-not-submitted comment. Can you either 1) make it so you can cmd + click "Show Notes" and open a new browser window with the show notes page or 2) make is so you can click back to return to this form without loosing the form data?
Here is my comment again, though I'm going to paraphrase this time:
I believe you can use
factories.rband your spec files. For example:
I prefer not to include
spec_helper.rb, as I like to explicitly show that I'm using a factory to build/create objects. Just using
buildmight be misinterpreted as ActiveRecord calls.
especially if you put these into rspec
Quick question, I'm currently writing integration tests for a Sorcery powered Rails 3.1 app. I'm having some issues when trying to fill in the login form. This is how my test is set up:
That current test setup doesn't work; however, if I do:
It does work. Just wondering why this isn't working for me. I assume it has something to do with encrypting the password during setup or something.
Here's my factory if anyone is interested:
What about doing stubs and mock objects for request specs? I know it's possible in FactoryGirl but I never quite got the hang of it
Just a remark on the screencast.
Apparently, you need to use
I had this same issue with factory_girl 3.5.0 and factory_girl_rails 3.5.0.
I want to use the factories.rb file exclusively. However, when I run rails generate model User ... I get a spec/factories/user.rb file. How can I configure it so that this does not happen?
Thank you and keep up the great work you're doing with this site.
I'd like to be able to create two objects, A and B, and relate them both to Object C in my factories so I can dry up my code. How can I do that using factory girl?
I'm running into scenarios where Object A is related to one instance of Object C, and Object B is related to another instance of Object C, when I'd like them to both be relating to the same instance of Object C.
Hey John, did you ever figure this out?
Thanks for this episode. Ryan's statement about not separating test code and test data makes sense. How about shared factories across tests? Is that a bad idea?
This episode has been updated to Rails 5 as a blog post Factories in Rails 5