False Positive Tests in Rails, ProTip

False positive tests are a special kind of evil. This is the kind of stuff that will cost you your annual raise.

One of the ways this has recently crept up and done me wrong is very intuitive, yet so hard to find if you look in all the wrong places: caching.

Specifically:

# app/controllers/your_controller.rb

Rails.cache.fetch(you_key) do
  # testable code
end

At this point, make sure your environment is set up to do what you want:

# environments/test.rb

config.action_controller.perform_caching = false

Also take a look at my new and improved test helper. I use TestUnit with Shoulda:

# test/test_helper.rb
class ActiveSupport::TestCase
  setup do      
    Rails.cache.clear
  end
end

class ActionController::TestCase
  setup do      
    Rails.cache.clear
  end
end

Good luck, hope you never trick yourself into false confidence inspiring tests.