Search code examples
testingruby-on-rails-3.2ruby-on-rails-3.1capybaraasset-pipeline

Testing the asset pipeline with Capybara


I want to do simple request specs in my Rails 3.1 application with Capybara. The standard cases all work as expected, but when I want to test CSS generated by the asset pipeline, I receive the following error:

Failure/Error: visit '/assets/main.css'
ActionController::RoutingError:
  No route matches [GET] "/assets/main.css"

I think the problem is that the test environment does not provide a complete server and so also no Sprockets middleware delivering the assets. Is there a solution to this problem?

EDIT: Now possible! We updated to Rails 3.2.12 and Capybara 2.0.2, now the assets are also available in the feature specs.


Solution

  • The Phusion guys blogged about a possibility to render an asset to a string:

    MyApp::Application.assets.find_asset('main.css').body
    

    You can also use this in tests. The solution is not ideal and/since Capybara isn't involved anymore, but it helps in my specific case to validate CSS. Better approaches are welcome!