Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec 2: How to render_views by default for all controller specs

I'm always writing render_views in all my controller specs:

require 'spec_helper'

describe AwesomeController do
  render_views
end

Is there any way to always render views on all controller specs?

like image 811
knoopx Avatar asked Dec 09 '10 18:12

knoopx


3 Answers

The documented way to do so, as of today is the following

spec/support/render_views.rb

RSpec.configure do |config|
  config.render_views
end
like image 135
bartocc Avatar answered Nov 10 '22 09:11

bartocc


Add this to spec/spec_helper.rb:

config.include(Module.new {
  def self.included(base)
    base.render_views
  end
}, :type => :controller)

It creates an anonymous module, that runs render_views on the class it is included in, and it is included on any describe-block that describes a controller.

like image 33
iain Avatar answered Nov 10 '22 11:11

iain


Add It To Your spec_helper.rb Config.

You can add render_views to your rspec config, like so:

In Your spec_helper.rb:

RSpec.configure do |config|

  # Renders views in controllers.
  config.render_views

  # Other config setup.

end

Turning Off render_views.

You can turn off view rendering on a per describe/context basis with render_views false, like so:

context "without view rendering even with global render_views on" do
  render_views false

  # specs without view rendering.
end
like image 37
Joshua Pinter Avatar answered Nov 10 '22 09:11

Joshua Pinter