markevans/dragonfly — Issue #171: Rack::Cache works in test environment
Author: semaperepelitsa
Created: 2012-02-21 11:20:54+00:00
State: open

Dragonfly inserts Rack::Cache middleware in all environments when requiring "dragonfly/images". This is fine for development environment but causes troubles in Capybara integration tests. Let's say, I have a CategoriesController with index action that uses HTTP caching.

```ruby
def index
expires_in 5.minutes, :public => true
end
```

When I `visit` the page in Capybara integration test (that in turn uses Rack::Test), Rack::Cache stores it and returns it for the rest of tests which usually want fresh pages each.

```ruby
def test_multiple_categories
Category.create!(name: "Books")
Category.create!(name: "Pets")
visit categories_path
assert has_content?("Books")
assert has_content?("Pets")
end

def test_no_categories
visit categories_path
assert has_content?("No categories")
end
```

In the above example one of tests will get a stale page and will fail. It took me a while to realize why the tests were randomly failing. Now I have the following initializer that removes Rack::Cache middleware in test environment:

```ruby
Rails.application.middleware.delete(Rack::Cache) if Rails.env.test?
```

I think that Dragonfly should not include Rack::Cache in test environment.