123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: rspec-rails/lib/rspec/rails/example/view_example_group.rb

Overview

DSL exposed to view specs.

Instance Method Summary

DSL Calls

included

[ GitHub ]


44
45
46
47
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 44

included do
  include ::Rails.application.routes.url_helpers
  include ::Rails.application.routes.mounted_helpers
end

Instance Method Details

#_controller_path (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 166

def _controller_path
  _path_parts[0..-2].join("/")
end

#_default_render_options (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 128

def _default_render_options
  formats = if ActionView::Template::Types.respond_to?(:symbols)
              ActionView::Template::Types.symbols
            else
              [:html, :text, :js, :css, :xml, :json].map(&:to_s)
            end.map { |x| Regexp.escape(x) }.join("|")

  handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
  locales = "[a-z]{2}(?:-[A-Z]{2})?"
  variants = "[^.]*"
  path_regex = %r{
  \A
  (?<template>.*?)
  (?:\.(?<locale>#{locales}))??
    (?:\.(?<format>#{formats}))??
    (?:\+(?<variant>#{variants}))??
    (?:\.(?<handler>#{handlers}))?
    \z
  }x

  # This regex should always find a match.
  # Worst case, everything will be nil, and :template will just be
  # the original string.
  match = path_regex.match(_default_file_to_render)

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

  render_options
end

#_include_controller_helpers (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 174

def _include_controller_helpers
  helpers = controller._helpers
  view.singleton_class.class_exec do
    include helpers unless included_modules.include?(helpers)
  end
end

#_inferred_action (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 170

def _inferred_action
  _path_parts.last.split(".").first
end

#_path_parts (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 162

def _path_parts
  _default_file_to_render.split("/")
end

#params

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

params[:foo] = 'bar'
[ GitHub ]

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

def params
  controller.params
end

#render #render({partial: path_to_file}) #render({partial: path_to_file}, {... locals ...}) #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
[ GitHub ]

  
# File 'rspec-rails/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

#response

Deprecated.

Use ‘rendered` instead.

[ GitHub ]

  
# File 'rspec-rails/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)

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.")
[ GitHub ]

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

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

#template

Deprecated.

Use ‘view` instead.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/view_example_group.rb', line 105

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

#view

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
[ GitHub ]

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

def view
  _view
end