Quantcast
Channel: A Waage Blog » RSpec
Viewing all articles
Browse latest Browse all 4

Rails testing with Machinist 2, Rspec, Database Cleaner Gem

$
0
0

QUICK vent and advice when using Machinst2 and Database Cleaner to test in Rails:

TURN OFF MACHINIST CACHING!

Add this to your environments/test.rb file:

Machinist.configure do |config|
  config.cache_objects = false
end

Machinist tries to do some weird caching to make your tests run faster. But, it doesn’t quite work the way you’d expect. If you are running into strange problems where your objects are persisting through many tests, even though you are using DatabaseCleaner after each test, you might try this. If you run into problems where running one test at a time works, but running “rake spec” results in errors, this is also worth a shot. Don’t let Machinist caching drive you nuts! :)

Sidenote: In my experience, the best way to debug these errors that appear when running the entire test suite, but do not appear when running individual tests is to use rspec to run all but one test. Remove one at a time, and see if removing that single test helps eliminate errors.
Example:

# If this gives errors:
$ bundle exec rspec ./spec/models/user_spec.rb ./spec/models/account_spec.rb ./spec/models/favorite_spec.rb
# Try removing the first
 $ bundle exec rspec ./spec/models/account_spec.rb ./spec/models/favorite_spec.rb
# Try removing the 2nd
 $ bundle exec rspec ./spec/models/user_spec.rb ./spec/models/favorite_spec.rb
# Repeat...

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images