Capybara + RSpec only sees blank pages in controller specs. Why?

前端 未结 1 2040

I\'m trying to write a controller spec for a simple controller. However, Capybara isn\'t seeing any page content. However, looking at the site\'s pages in my browser works j

相关标签:
1条回答
  • 2021-01-12 18:26

    You need to explicitly tell your controller spec that you want it to render views in order for this to work. Update your spec to look like this:

    require 'spec_helper'
    
    describe PostsController do
      render_views # Render this controller's views during spec execution.
    
      before do
        @post = Fabricate :post
      end
    
      # ...
    end
    

    This is described in rspec's readme. For a more detailed view, see rspec-rails' cucumber feature for 'render_views'.

    Just one word of caution with this. There are reasons why this isn't default behaviour:

    • Arguably, you're mixing two concerns by testing the views at the same time as the controllers. Ryan Bigg (see comments) suggests your tests might be better thought of as integration tests, which usually live in spec/integration rather than spec/controller.
    • Rendering the views may slow down the execution of your tests considerably.

    ... Not saying you shouldn't do this, just saying you should be clear why you are.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题