Class Spec::Rails::Example::ViewExampleGroup

  1. lib/spec/rails/example/view_example_group.rb

View Examples live in $RAILS_ROOT/spec/views/.

View Specs use Spec::Rails::Example::ViewExampleGroup, which provides access to views without invoking any of your controllers. See Spec::Rails::Expectations::Matchers for information about specific expectations that you can set on views.

Example

describe "login/login" do
  before do
    render 'login/login'
  end

  it "should display login form" do
    response.should have_tag("form[action=/login]") do
      with_tag("input[type=text][name=email]")
      with_tag("input[type=password][name=password]")
      with_tag("input[type=submit][value=Login]")
    end
  end
end

Methods

public instance

  1. render
  2. template

protected instance

  1. _assigns_hash_proxy

Public instance methods

render (*args)

Renders a template for a View Spec, which then provides access to the result through the response. Also supports render with :inline, which you can use to spec custom form builders, helpers, etc, in the context of a view.

Examples

render('/people/list')
render('/people/list', :helper => MyHelper)
render('/people/list', :helpers => [MyHelper, MyOtherHelper])
render(:partial => '/people/_address')
render(:inline => "<% custom_helper 'argument', 'another argument' %>")

See Spec::Rails::Example::ViewExampleGroup for more information.

[show source]
# File lib/spec/rails/example/view_example_group.rb, line 120
        def render(*args)
          options = Hash === args.last ? args.pop : {}
          
          if args.empty? 
            unless [:partial, :inline, :file, :template, :xml, :json, :update].any? {|k| options.has_key? k} 
              args << self.class.description_parts.first
            end
          end
          
          options[:template] = args.first.to_s.sub(/^\//,'') unless args.empty?
          
          set_base_view_path(options)
          add_helpers(options)

          assigns[:action_name] = @action_name
          
          @request.path_parameters = @request.path_parameters.merge(
            :controller => derived_controller_name(options),
            :action => derived_action_name(options)
          ).merge(options[:path_parameters] || {})

          defaults = { :layout => false }
          options = defaults.merge options

          @controller.__send__(:params).reverse_merge! @request.parameters

          @controller.class.instance_eval %{
            def controller_path
              "#{derived_controller_name(options)}"
            end

            def controller_name
              "#{derived_controller_name(options).split('/').last}"
            end
          }

          @controller.__send__ :forget_variables_added_to_assigns
          @controller.__send__ :render, options
          @controller.__send__ :process_cleanup
        end
template ()

This provides the template. Use this to set mock expectations for dealing with partials

Example

describe "/person/new" do
  it "should use the form partial" do
    template.should_receive(:render).with(:partial => 'form')
    render "/person/new"
  end
end
[show source]
# File lib/spec/rails/example/view_example_group.rb, line 172
        def template
          @controller.template
        end

Protected instance methods

_assigns_hash_proxy ()
[show source]
# File lib/spec/rails/example/view_example_group.rb, line 179
        def _assigns_hash_proxy
          @_assigns_hash_proxy ||= AssignsHashProxy.new(self) {@response.template}
        end