Module: RSpec::Rails::ViewExampleGroup::ExampleMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/rspec/rails/example/view_example_group.rb

Overview

DSL exposed to view specs.

Instance Method Summary collapse

Instance Method Details

#paramsObject

Provides access to the params hash that will be available within the view.

params[:foo] = 'bar'


100
101
102
# File 'lib/rspec/rails/example/view_example_group.rb', line 100

def params
  controller.params
end

#renderObject #render({partial: path_to_file}) ⇒ Object #render({partial: path_to_file}, {... locals ...}) ⇒ Object #render({partial: path_to_file}, {... locals ...}) ⇒ Object

Delegates to ActionView::Base#render, so see documentation on that for more info.

The only addition is that you can call render with no arguments, and RSpec will pass the top level description to render:

describe "widgets/new.html.erb" do
  it "shows all the widgets" do
    render # => view.render(file: "widgets/new.html.erb")
    # ...
  end
end


66
67
68
69
70
# File 'lib/rspec/rails/example/view_example_group.rb', line 66

def render(options = {}, local_assigns = {}, &block)
  options = _default_render_options if Hash === options && options.empty?
  options = options.merge(_default_render_options) if Hash === options && options.keys == [:locals]
  super(options, local_assigns, &block)
end

#responseObject

Deprecated.

Use rendered instead.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rspec/rails/example/view_example_group.rb', line 111

def response
  # `assert_template` expects `response` to implement a #body method
  # like an `ActionDispatch::Response` does to force the view to
  # render. For backwards compatibility, we use #response as an alias
  # for #rendered, but it needs to implement #body to avoid
  # `assert_template` raising a `NoMethodError`.
  unless rendered.respond_to?(:body)
    def rendered.body
      self
    end
  end

  rendered
end

#stub_template(hash) ⇒ Object

Simulates the presence of a template on the file system by adding a Rails' FixtureResolver to the front of the view_paths list. Designed to help isolate view examples from partials rendered by the view template that is the subject of the example.

stub_template("widgets/_widget.html.erb" => "This content.")


92
93
94
# File 'lib/rspec/rails/example/view_example_group.rb', line 92

def stub_template(hash)
  controller.prepend_view_path(StubResolverCache.resolver_for(hash))
end

#templateObject

Deprecated.

Use view instead.



105
106
107
108
# File 'lib/rspec/rails/example/view_example_group.rb', line 105

def template
  RSpec.deprecate("template", replacement: "view")
  view
end

#viewObject

The instance of ActionView::Base that is used to render the template. Use this to stub methods before calling render.

describe "widgets/new.html.erb" do
  it "shows all the widgets" do
    view.stub(:foo) { "foo" }
    render
    # ...
  end
end


82
83
84
# File 'lib/rspec/rails/example/view_example_group.rb', line 82

def view
  _view
end