Method: Test::Rails::ViewTestCase#render

Defined in:
lib/test/rails/view_test_case.rb

#render(options = {}, deprecated_status = nil) ⇒ Object

Renders the template. The template is determined from the test name. If you have multiple tests for the same view render will try to Do The Right Thing and remove parts of the name looking for the template file.

By default, render has the added option :layout => false, so if want to test behavior in your layout add :layout => true.

The action can be forced by using the options:

render :action => 'new'

render :template => 'profile/index'

A test’s path parameters may be overridden, allowing routes with additional parameters to work.

Working with Routes

By default, a view tests sets the controller and action of a test to the controller name and action name for the test. This may be overriden.

A test involving routes like:

map.workspace '/users/:owner/workspace/:action',
              :controller => 'workspace', :action => 'workspace'

Can be invoked by setting @path_parameters like this:

def test__app_entry
  @path_parameters[:owner] = 'bob'
  @path_parameters[:action] = 'apps'

  render :partial => 'apps/app_entry'

  # ...
end

View Lookup

render strips off words trailing an _ in the test name one at a time until it finds a matching action. It tries the extensions ‘rhtml’, ‘rxml’, ‘rjs’, and ‘mab’ in order for each action until a view is found.

With this test case:

class RouteViewTest < Test::Rails::ViewTestCase
  def test_show_photos
    render
  end
  def test_show_no_photos
    render
  end
end

In test_show_photos, render will look for:

  • app/views/route/show_photos.rhtml

  • app/views/route/show_photos.rxml

  • app/views/route/show_photos.rjs

  • app/views/route/show_photos.mab

  • app/views/route/show.

And in test_show_no_photos, render will look for:

If a view cannot be found the test will flunk.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/test/rails/view_test_case.rb', line 207

def render(options = {}, deprecated_status = nil)
  @action_name = action_name caller[0] if options.empty?
  assigns[:action_name] = @action_name

  default_path_parameters = {
    :controller => @controller.controller_name,
    :action => @action_name
  }

  path_parameters = default_path_parameters.merge(@path_parameters)

  @request.path_parameters = path_parameters

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

  if Test::Rails.rails_version >= Test::Rails.v1_2 then
    @controller.send :params=, @request.parameters
  else
    @controller.instance_variable_set :@params, @request.parameters
  end
  @controller.send :initialize_current_url
  current_url = URI.parse @controller.url_for
  @request.request_uri = current_url.request_uri

  # Rails 1.0
  @controller.send :assign_names rescue nil
  @controller.send :fire_flash rescue nil

  # Rails 1.1
  @controller.send :forget_variables_added_to_assigns rescue nil

  # Do the render
  options[:TR_force] = true
  @controller.render options, deprecated_status

  # Rails 1.1
  @controller.send :process_cleanup rescue nil
end