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

可紊 提交于 2019-12-01 05:17:52

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!