- Use SQLite in memory for your test env.
1 2 3
That’s the most straightforward thing to do, but unfortunately, if you are sharing your test env with Cucumber, you might want to use a production like DB with Cucumber (PostgreSQL or whatever). So unless you are ready to setup a new env for cucumber (which I tried and don’t recommend) you’re stuck.
- Use mocks. That’s surely going to work, it’s going to make your test hell of a lot faster ! It will also make your tests a lot more fragile and more expensive to maintain … If you want to read more about why I think mocks are a bad idea, just have a look at these posts.
Here is a third alternative, I’ve already written about it, but here it comes updated and tested for Rails 5 :
- Don’t change anything to your
- Obviously, you’ll need to add
- At the beginning of your
1 2 3
1 2 3 4 5
That’s it ! Run your specs … not bad for a 5 minutes investment !
Rails 5.1 (2017-03-29 Edit)
My fresh hack started to fail on Rails 5.1 ! If
schema.rb is generated with the Postgres adapter, it is now incompatible with this injected Sqlite adapter. Here is a patch that removes the glitches :
1 2 3 4 5 6 7
I admit this is getting a bit crappy, and I don’t know how long it is going to work …
One more thing …
If you need even more speed, you can now run your specs in parallel in different processes ! Each in-memory SQLite DB is bound to its process, so unlike a real PostgreSQL dev DB, you won’t get any conflicts between your tests ;–)