aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/scaffold_controller_generator_test.rb
blob: ed54ce43da9b103e472f7f7fc393d642ab87cbfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'

module Unknown
  module Generators
  end
end

class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  arguments %w(User name:string age:integer)

  def test_controller_skeleton_is_created
    run_generator

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.all/, m)
      end

      assert_instance_method :show, content do |m|
        assert_match(/@user = User\.find\(params\[:id\]\)/, m)
      end

      assert_instance_method :new, content do |m|
        assert_match(/@user = User\.new/, m)
      end

      assert_instance_method :edit, content do |m|
        assert_match(/@user = User\.find\(params\[:id\]\)/, m)
      end

      assert_instance_method :create, content do |m|
        assert_match(/@user = User\.new\(params\[:user\]\)/, m)
        assert_match(/@user\.save/, m)
        assert_match(/@user\.errors/, m)
      end

      assert_instance_method :update, content do |m|
        assert_match(/@user = User\.find\(params\[:id\]\)/, m)
        assert_match(/@user\.update_attributes\(params\[:user\]\)/, m)
        assert_match(/@user\.errors/, m)
      end

      assert_instance_method :destroy, content do |m|
        assert_match(/@user = User\.find\(params\[:id\]\)/, m)
        assert_match(/@user\.destroy/, m)
      end
    end
  end

  def test_helper_are_invoked_with_a_pluralized_name
    run_generator
    assert_file "app/helpers/users_helper.rb", /module UsersHelper/
    assert_file "test/unit/helpers/users_helper_test.rb", /class UsersHelperTest < ActionView::TestCase/
  end

  def test_views_are_generated
    run_generator

    %w(
      index
      edit
      new
      show
    ).each { |view| assert_file "app/views/users/#{view}.html.erb" }
    assert_no_file "app/views/layouts/users.html.erb"
  end

  def test_functional_tests
    run_generator

    assert_file "test/functional/users_controller_test.rb" do |content|
      assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
      assert_match(/test "should get index"/, content)
      assert_match(/post :create, user: \{ age: @user.age, name: @user.name \}/, content)
      assert_match(/put :update, id: @user, user: \{ age: @user.age, name: @user.name \}/, content)
    end
  end

  def test_functional_tests_without_attributes
    run_generator ["User"]

    assert_file "test/functional/users_controller_test.rb" do |content|
      assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
      assert_match(/test "should get index"/, content)
      assert_match(/post :create, user: \{  \}/, content)
      assert_match(/put :update, id: @user, user: \{  \}/, content)
    end
  end

  def test_skip_helper_if_required
    run_generator ["User", "name:string", "age:integer", "--no-helper"]
    assert_no_file "app/helpers/users_helper.rb"
    assert_no_file "test/unit/helpers/users_helper_test.rb"
  end

  def test_skip_layout_if_required
    run_generator ["User", "name:string", "age:integer", "--no-layout"]
    assert_no_file "app/views/layouts/users.html.erb"
  end

  def test_default_orm_is_used
    run_generator ["User", "--orm=unknown"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.all/, m)
      end
    end
  end

  def test_customized_orm_is_used
    klass = Class.new(Rails::Generators::ActiveModel) do
      def self.all(klass)
        "#{klass}.find(:all)"
      end
    end

    Unknown::Generators.const_set(:ActiveModel, klass)
    run_generator ["User", "--orm=unknown"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/class UsersController < ApplicationController/, content)

      assert_instance_method :index, content do |m|
        assert_match(/@users = User\.find\(:all\)/, m)
        assert_no_match(/@users = User\.all/, m)
      end
    end
  ensure
    Unknown::Generators.send :remove_const, :ActiveModel
  end

  def test_new_hash_style
    run_generator
    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/\{ render action: "new" \}/, content)
    end
  end

  def test_http_only_generates_controller_without_respond_to_block_and_html_format
    run_generator ["User", "--http"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/render json: @user/, content)
      assert_no_match(/respond_to/, content)
      assert_no_match(/format\.html/, content)
    end
  end

  def test_http_only_generates_functional_tests_with_json_format_and_http_status_assertions
    run_generator ["User", "--http"]

    assert_file "test/functional/users_controller_test.rb" do |content|
      assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
      assert_match(/@request\.accept = "application\/json"/, content)
      assert_match(/test "should get index"/, content)

      assert_match(/assert_response 201/, content)
      assert_no_match(/assert_redirected_to/, content)
    end
  end

  def test_http_only_does_not_generate_edit_action
    run_generator ["User", "--http"]

    assert_file "app/controllers/users_controller.rb" do |content|
      assert_match(/def index/, content)
      assert_no_match(/def edit/, content)
    end

    assert_file "test/functional/users_controller_test.rb" do |content|
      assert_match(/test "should get index"/, content)
      assert_no_match(/test "should get edit"/, content)
    end
  end
end