module RSpec::Rails::ViewExampleGroup::ExampleMethods

DSL exposed to view specs.

Public Instance Methods

params() click to toggle source

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

params[:foo] = 'bar'
# File lib/rspec/rails/example/view_example_group.rb, line 94
def params
  controller.params
end
render(options = {}, local_assigns = {}, &block) click to toggle source

@overload render @overload render({:partial => path_to_file}) @overload render({:partial => path_to_file}, {… locals …}) @overload render({:partial => path_to_file}, {… locals …}) do … end

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
Calls superclass method
# File lib/rspec/rails/example/view_example_group.rb, line 61
def render(options = {}, local_assigns = {}, &block)
  options = _default_render_options if Hash === options && options.empty?
  super(options, local_assigns, &block)
end
response() click to toggle source

@deprecated Use `rendered` instead.

# File lib/rspec/rails/example/view_example_group.rb, line 105
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) click to toggle source

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.")
# File lib/rspec/rails/example/view_example_group.rb, line 86
def stub_template(hash)
  view.view_paths.unshift(ActionView::FixtureResolver.new(hash))
end
template() click to toggle source

@deprecated Use `view` instead.

# File lib/rspec/rails/example/view_example_group.rb, line 99
def template
  RSpec.deprecate("template", :replacement => "view")
  view
end
view() click to toggle source

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
# File lib/rspec/rails/example/view_example_group.rb, line 76
def view
  _view
end

Private Instance Methods

_controller_path() click to toggle source
# File lib/rspec/rails/example/view_example_group.rb, line 143
def _controller_path
  _path_parts[0..-2].join("/")
end
_default_render_options() click to toggle source
# File lib/rspec/rails/example/view_example_group.rb, line 122
def _default_render_options
  if ::Rails::VERSION::STRING >= '3.2'
    # pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
    template, *components   = _default_file_to_render.split('.')
    handler, format, locale = *components.reverse

    render_options = { :template => template }
    render_options[:handlers] = [handler] if handler
    render_options[:formats] = [format.to_sym] if format
    render_options[:locales] = [locale] if locale

    render_options
  else
    { :template => _default_file_to_render }
  end
end
_include_controller_helpers() click to toggle source
# File lib/rspec/rails/example/view_example_group.rb, line 151
def _include_controller_helpers
  helpers = controller._helpers
  view.singleton_class.class_exec do
    include helpers unless included_modules.include?(helpers)
  end
end
_inferred_action() click to toggle source
# File lib/rspec/rails/example/view_example_group.rb, line 147
def _inferred_action
  _path_parts.last.split(".").first
end
_path_parts() click to toggle source
# File lib/rspec/rails/example/view_example_group.rb, line 139
def _path_parts
  _default_file_to_render.split("/")
end