Question

  1. I have a couple of complicated objects to stub out (instances of gems I use). Where can I centralize these stubs to make them available to all tests?

  2. How can I programatically clear the DB between tests without rake:test? I want to quickly run individual tests through textmate, but doing so will error out since it doesn't clear the DB between tests

  3. The tests run slow since it has to spin up a Rails instance. How to speed up the tests? Especially while writing the tests and wanting to quickly run changes

Was it helpful?

Solution

1) You can either put them in test_helper.rb to make them available to all tests or you could write your own module which contains those methods and then include that module in the tests that require those stubs.

2) You could put Model.destroy_all (or .delete_all if appropriate which would be quicker) in your test setup method to strip out those models that you are no longer interested in.

However, if you are running tests in transactions (and your database supports transactions) then you shouldn't need to clear out any data because the creation of the data and the test will run in a transaction which will then be rolled back clearing the data automatically.

3) Not so sure on this one. I had this problem a lot developing on Windows but not so much on *nix. You could look into some kind of continuous testing but there's still going to be a delay on feedback. It might be worth investigating what is causing the rails environment to be so slow starting - it might be something you can skip in your testing environment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top