functional_test.rb 2.3 KB
Newer Older
1
require File.dirname(__FILE__) + '/../test_helper'
2
require '<%= controller_file_name %>_controller'
3 4

# Re-raise errors caught by the controller.
5
class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6

7
class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8 9 10
  fixtures :<%= table_name %>

  def setup
11 12
    $base_id = 1000001
    @controller = <%= controller_class_name %>Controller.new
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
  end

<% for action in unscaffolded_actions -%>
  def test_<%= action %>
    process :<%= action %>
    assert_rendered_file '<%= action %>'
  end

<% end -%>
<% unless suffix -%>
  def test_index
    process :index
    assert_rendered_file 'list'
  end

<% end -%>
  def test_list<%= suffix %>
    process :list<%= suffix %>
    assert_rendered_file 'list<%= suffix %>'
    assert_template_has '<%= plural_name %>'
  end

  def test_show<%= suffix %>
37
    process :show<%= suffix %>, 'id' => $base_id
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    assert_rendered_file 'show'
    assert_template_has '<%= singular_name %>'
    assert_valid_record '<%= singular_name %>'
  end

  def test_new<%= suffix %>
    process :new<%= suffix %>
    assert_rendered_file 'new<%= suffix %>'
    assert_template_has '<%= singular_name %>'
  end

  def test_create
    num_<%= plural_name %> = <%= class_name %>.find_all.size

    process :create<%= suffix %>, '<%= singular_name %>' => { }
    assert_redirected_to :action => 'list<%= suffix %>'

    assert_equal num_<%= plural_name %> + 1, <%= class_name %>.find_all.size
  end

  def test_edit<%= suffix %>
59
    process :edit<%= suffix %>, 'id' => $base_id
60 61 62 63 64 65
    assert_rendered_file 'edit<%= suffix %>'
    assert_template_has '<%= singular_name %>'
    assert_valid_record '<%= singular_name %>'
  end

  def test_update<%= suffix %>
66 67
    process :update<%= suffix %>, '<%= singular_name %>' => { 'id' => $base_id }
    assert_redirected_to :action => 'show<%= suffix %>', :id => $base_id
68 69 70
  end

  def test_destroy<%= suffix %>
71
    assert_not_nil <%= class_name %>.find($base_id)
72

73
    process :destroy, 'id' => $base_id
74 75 76
    assert_redirected_to :action => 'list<%= suffix %>'

    assert_raise(ActiveRecord::RecordNotFound) {
77
      <%= singular_name %> = <%= class_name %>.find($base_id)
78 79 80
    }
  end
end