minitest-rails 2.2.1 → 3.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -6
  3. data/CHANGELOG.rdoc +0 -6
  4. data/Gemfile +1 -1
  5. data/Manifest.txt +6 -13
  6. data/Rakefile +4 -3
  7. data/gemfiles/5.0.gemfile +5 -0
  8. data/gemfiles/head.gemfile +5 -0
  9. data/lib/generators/minitest/controller/templates/controller_spec.rb +9 -5
  10. data/lib/generators/minitest/controller/templates/controller_test.rb +9 -5
  11. data/lib/generators/minitest/generator/generator_generator.rb +2 -3
  12. data/lib/generators/minitest/generator/templates/generator_spec.rb +1 -1
  13. data/lib/generators/minitest/generator/templates/generator_test.rb +1 -1
  14. data/lib/generators/minitest/helper/helper_generator.rb +1 -11
  15. data/lib/generators/minitest/integration/integration_generator.rb +1 -2
  16. data/lib/generators/minitest/integration/templates/integration_spec.rb +3 -3
  17. data/lib/generators/minitest/integration/templates/integration_test.rb +3 -3
  18. data/lib/generators/minitest/job/job_generator.rb +2 -3
  19. data/lib/generators/minitest/job/templates/job_spec.rb +3 -3
  20. data/lib/generators/minitest/job/templates/job_test.rb +3 -3
  21. data/lib/generators/minitest/mailer/mailer_generator.rb +9 -4
  22. data/lib/generators/minitest/mailer/templates/mailer_spec.rb +4 -4
  23. data/lib/generators/minitest/mailer/templates/mailer_test.rb +5 -5
  24. data/lib/generators/minitest/mailer/templates/preview.rb +5 -5
  25. data/lib/generators/minitest/model/model_generator.rb +3 -4
  26. data/lib/generators/minitest/model/templates/fixtures.yml +17 -12
  27. data/lib/generators/minitest/scaffold/scaffold_generator.rb +24 -3
  28. data/lib/generators/minitest/scaffold/templates/api_controller_spec.rb +42 -0
  29. data/lib/generators/minitest/scaffold/templates/api_controller_test.rb +44 -0
  30. data/lib/generators/minitest/scaffold/templates/controller_spec.rb +14 -11
  31. data/lib/generators/minitest/scaffold/templates/controller_test.rb +17 -14
  32. data/lib/minitest/rails.rb +10 -27
  33. data/lib/minitest/rails/assertions.rb +0 -561
  34. data/lib/minitest/rails/expectations.rb +0 -63
  35. data/lib/minitest/rails/version.rb +1 -1
  36. data/minitest-rails.gemspec +13 -10
  37. data/tasks/test.rake +8 -8
  38. data/test/generators/test_helper_generator.rb +8 -16
  39. data/test/generators/test_mailer_generator.rb +16 -17
  40. data/test/generators/test_model_generator.rb +1 -1
  41. data/test/generators/test_scaffold_generator.rb +2 -2
  42. data/test/helper.rb +16 -1
  43. data/test/rails/{action_controller → action_dispatch}/test_assertions.rb +4 -11
  44. data/test/rails/{action_controller → action_dispatch}/test_expectations.rb +4 -11
  45. data/test/rails/action_dispatch/test_spec_type.rb +6 -18
  46. data/test/rails/action_mailer/test_mailers.rb +6 -6
  47. data/test/rails/action_view/test_helpers.rb +2 -2
  48. metadata +28 -21
  49. data/gemfiles/4.1.gemfile +0 -5
  50. data/gemfiles/4.2.gemfile +0 -5
  51. data/lib/generators/minitest/helper/templates/helper_spec.rb +0 -9
  52. data/lib/generators/minitest/helper/templates/helper_test.rb +0 -9
  53. data/lib/generators/minitest/route/route_generator.rb +0 -28
  54. data/lib/generators/minitest/route/templates/route_spec.rb +0 -13
  55. data/lib/generators/minitest/route/templates/route_test.rb +0 -13
  56. data/test/generators/test_route_generator.rb +0 -24
  57. data/test/rails/action_controller/test_controllers.rb +0 -202
  58. data/test/rails/action_controller/test_spec_type.rb +0 -47
  59. data/test/rails/minitest_5_api_test.rb +0 -8
@@ -1,24 +1,29 @@
1
- # Read about fixtures at
2
- # http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
3
-
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
4
2
  <% unless attributes.empty? -%>
5
- one:
3
+ <% %w(one two).each do |name| %>
4
+ <%= name %>:
6
5
  <% attributes.each do |attribute| -%>
7
- <%= attribute.name %>: <%= attribute.default %>
6
+ <%- if attribute.password_digest? -%>
7
+ password_digest: <%%= BCrypt::Password.create("secret") %>
8
+ <%- elsif attribute.reference? -%>
9
+ <%= yaml_key_value(attribute.column_name.sub(/_id$/, ""), attribute.default || name) %>
10
+ <%- else -%>
11
+ <%= yaml_key_value(attribute.column_name, attribute.default) %>
12
+ <%- end -%>
13
+ <%- if attribute.polymorphic? -%>
14
+ <%= yaml_key_value("#{attribute.name}_type", attribute.human_name) %>
15
+ <%- end -%>
8
16
  <% end -%>
9
-
10
- two:
11
- <% attributes.each do |attribute| -%>
12
- <%= attribute.name %>: <%= attribute.default %>
13
17
  <% end -%>
14
18
  <% else -%>
15
- # This model initially had no columns defined. If you add columns to the
16
- # model remove the '{}' from the fixture names and add the columns immediately
19
+
20
+ # This model initially had no columns defined. If you add columns to the
21
+ # model remove the "{}" from the fixture names and add the columns immediately
17
22
  # below each fixture, per the syntax in the comments below
18
23
  #
19
24
  one: {}
20
25
  # column: value
21
26
  #
22
27
  two: {}
23
- # column: value
28
+ # column: value
24
29
  <% end -%>
@@ -8,18 +8,38 @@ module Minitest
8
8
 
9
9
  check_class_collision suffix: "ControllerTest"
10
10
 
11
+ class_option :api, type: :boolean,
12
+ desc: "Generates API functional tests"
13
+
11
14
  argument :attributes, type: :array, default: [], banner: "field:type field:type"
12
15
 
13
16
  def create_test_files
14
- if options[:spec]
15
- template_file = "controller_spec.rb"
17
+ if options.api?
18
+ if options[:spec]
19
+ template_file = "api_controller_spec.rb"
20
+ else
21
+ template_file = "api_controller_test.rb"
22
+ end
16
23
  else
17
- template_file = "controller_test.rb"
24
+ if options[:spec]
25
+ template_file = "controller_spec.rb"
26
+ else
27
+ template_file = "controller_test.rb"
28
+ end
18
29
  end
19
30
  template template_file,
20
31
  File.join("test/controllers", controller_class_path, "#{controller_file_name}_controller_test.rb")
21
32
  end
22
33
 
34
+ def fixture_name
35
+ @fixture_name ||=
36
+ if mountable_engine?
37
+ "%s_%s" % [namespaced_path, table_name]
38
+ else
39
+ table_name
40
+ end
41
+ end
42
+
23
43
  private
24
44
 
25
45
  def attributes_hash
@@ -29,6 +49,7 @@ module Minitest
29
49
  if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?)
30
50
  "#{name}: 'secret'"
31
51
  else
52
+ # don't use the ivar, use the accessor method
32
53
  "#{name}: #{singular_table_name}.#{name}"
33
54
  end
34
55
  end.sort.join(", ")
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest
5
+ <%- if mountable_engine? -%>
6
+ include Engine.routes.url_helpers
7
+
8
+ <%- end -%>
9
+ let(:<%= singular_table_name %>) { <%= fixture_name %> :one }
10
+
11
+ it "gets index" do
12
+ get <%= index_helper %>_url
13
+ value(response).must_be :success?
14
+ end
15
+
16
+ it "creates <%= singular_table_name %>" do
17
+ expect {
18
+ post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
19
+ }.must_change "<%= class_name %>.count"
20
+
21
+ value(response.status).must_equal 201
22
+ end
23
+
24
+ it "shows <%= singular_table_name %>" do
25
+ get <%= show_helper %>
26
+ value(response).must_be :success?
27
+ end
28
+
29
+ it "updates <%= singular_table_name %>" do
30
+ patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
31
+ value(response.status).must_equal 200
32
+ end
33
+
34
+ it "destroys <%= singular_table_name %>" do
35
+ expect {
36
+ delete <%= show_helper %>
37
+ }.must_change "<%= class_name %>.count", -1
38
+
39
+ value(response.status).must_equal 204
40
+ end
41
+ end
42
+ <% end -%>
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest
5
+ <%- if mountable_engine? -%>
6
+ include Engine.routes.url_helpers
7
+
8
+ <%- end -%>
9
+ def <%= singular_table_name %>
10
+ @<%= singular_table_name %> ||= <%= fixture_name %> :one
11
+ end
12
+
13
+ def test_index
14
+ get <%= index_helper %>_url
15
+ assert_response :success
16
+ end
17
+
18
+ def test_create
19
+ assert_difference('<%= class_name %>.count') do
20
+ post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
21
+ end
22
+
23
+ assert_response 201
24
+ end
25
+
26
+ def test_show
27
+ get <%= show_helper %>
28
+ assert_response :success
29
+ end
30
+
31
+ def test_update
32
+ patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
33
+ assert_response 200
34
+ end
35
+
36
+ def test_destroy
37
+ assert_difference('<%= class_name %>.count', -1) do
38
+ delete <%= show_helper %>
39
+ end
40
+
41
+ assert_response 204
42
+ end
43
+ end
44
+ <% end -%>
@@ -2,45 +2,48 @@ require "test_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
4
  describe <%= controller_class_name %>Controller do
5
- let(:<%= singular_table_name %>) { <%= table_name %> :one }
5
+ <%- if mountable_engine? -%>
6
+ include Engine.routes.url_helpers
7
+
8
+ <%- end -%>
9
+ let(:<%= singular_table_name %>) { <%= fixture_name %> :one }
6
10
 
7
11
  it "gets index" do
8
- get :index
12
+ get <%= index_helper %>_url
9
13
  value(response).must_be :success?
10
- value(assigns(:<%= table_name %>)).wont_be :nil?
11
14
  end
12
15
 
13
16
  it "gets new" do
14
- get :new
17
+ get <%= new_helper %>
15
18
  value(response).must_be :success?
16
19
  end
17
20
 
18
21
  it "creates <%= singular_table_name %>" do
19
22
  expect {
20
- post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
23
+ post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
21
24
  }.must_change "<%= class_name %>.count"
22
25
 
23
- must_redirect_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
26
+ must_redirect_to <%= singular_table_name %>_path(<%= class_name %>.last)
24
27
  end
25
28
 
26
29
  it "shows <%= singular_table_name %>" do
27
- get :show, id: <%= singular_table_name %>
30
+ get <%= show_helper %>
28
31
  value(response).must_be :success?
29
32
  end
30
33
 
31
34
  it "gets edit" do
32
- get :edit, id: <%= singular_table_name %>
35
+ get <%= edit_helper %>
33
36
  value(response).must_be :success?
34
37
  end
35
38
 
36
39
  it "updates <%= singular_table_name %>" do
37
- put :update, id: <%= singular_table_name %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
38
- must_redirect_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
40
+ patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
41
+ must_redirect_to <%= singular_table_name %>_path(<%= "#{singular_table_name}" %>)
39
42
  end
40
43
 
41
44
  it "destroys <%= singular_table_name %>" do
42
45
  expect {
43
- delete :destroy, id: <%= singular_table_name %>
46
+ delete <%= show_helper %>
44
47
  }.must_change "<%= class_name %>.count", -1
45
48
 
46
49
  must_redirect_to <%= index_helper %>_path
@@ -1,48 +1,51 @@
1
1
  require "test_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- class <%= controller_class_name %>ControllerTest < ActionController::TestCase
4
+ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest
5
+ <%- if mountable_engine? -%>
6
+ include Engine.routes.url_helpers
7
+
8
+ <%- end -%>
5
9
  def <%= singular_table_name %>
6
- @<%= singular_table_name %> ||= <%= table_name %> :one
10
+ @<%= singular_table_name %> ||= <%= fixture_name %> :one
7
11
  end
8
12
 
9
13
  def test_index
10
- get :index
14
+ get <%= index_helper %>_url
11
15
  assert_response :success
12
- assert_not_nil assigns(:<%= table_name %>)
13
16
  end
14
17
 
15
18
  def test_new
16
- get :new
19
+ get <%= new_helper %>
17
20
  assert_response :success
18
21
  end
19
22
 
20
23
  def test_create
21
- assert_difference("<%= class_name %>.count") do
22
- post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
24
+ assert_difference "<%= class_name %>.count" do
25
+ post <%= index_helper %>_url, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
23
26
  end
24
27
 
25
- assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
28
+ assert_redirected_to <%= singular_table_name %>_path(<%= class_name %>.last)
26
29
  end
27
30
 
28
31
  def test_show
29
- get :show, id: <%= singular_table_name %>
32
+ get <%= show_helper %>
30
33
  assert_response :success
31
34
  end
32
35
 
33
36
  def test_edit
34
- get :edit, id: <%= singular_table_name %>
37
+ get <%= edit_helper %>
35
38
  assert_response :success
36
39
  end
37
40
 
38
41
  def test_update
39
- put :update, id: <%= singular_table_name %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
40
- assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>))
42
+ patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }
43
+ assert_redirected_to <%= singular_table_name %>_path(<%= "#{singular_table_name}" %>)
41
44
  end
42
45
 
43
46
  def test_destroy
44
- assert_difference("<%= class_name %>.count", -1) do
45
- delete :destroy, id: <%= singular_table_name %>
47
+ assert_difference "<%= class_name %>.count", -1 do
48
+ delete <%= show_helper %>
46
49
  end
47
50
 
48
51
  assert_redirected_to <%= index_helper %>_path
@@ -1,9 +1,8 @@
1
1
  gem "minitest"
2
2
  require "minitest"
3
- require "minitest/test"
4
3
  require "minitest/spec"
5
4
  require "minitest/mock"
6
- require "minitest/autorun" unless ENV["MT_RAILS_NO_AUTORUN"]
5
+ require "minitest/hell" if ENV["MT_HELL"]
7
6
 
8
7
  ################################################################################
9
8
  # Add and configure the spec DSL
@@ -37,34 +36,13 @@ class ActiveSupport::TestCase
37
36
  end
38
37
  end
39
38
 
40
- require "action_controller/test_case"
41
- class ActionController::TestCase
42
- # Use AC::TestCase for the base class when describing a controller
43
- register_spec_type(self) do |desc|
44
- Class === desc && desc < ActionController::Metal
45
- end
46
- register_spec_type(/Controller( ?Test)?\z/i, self)
47
- register_spec_type(self) do |desc, *addl|
48
- addl.include? :controller
49
- end
50
-
51
- # Resolve the controller from the test name when using the spec DSL
52
- def self.determine_default_controller_class(name)
53
- controller = determine_constant_from_test_name(name) do |constant|
54
- Class === constant && constant < ActionController::Metal
55
- end
56
- raise NameError.new("Unable to resolve controller for #{name}") if controller.nil?
57
- controller
58
- end
59
- end
60
-
61
39
  require "action_view/test_case"
62
40
  class ActionView::TestCase
63
41
  # Use AV::TestCase for the base class for helpers and views
64
42
  register_spec_type(/(Helper( ?Test)?| View Test)\z/i, self)
65
43
  register_spec_type(self) do |desc, *addl|
66
44
  addl.include?(:view) ||
67
- addl.include?(:helper)
45
+ addl.include?(:helper)
68
46
  end
69
47
 
70
48
  # Resolve the helper or view from the test name when using the spec DSL
@@ -114,10 +92,15 @@ end
114
92
 
115
93
  require "action_dispatch/testing/integration"
116
94
  class ActionDispatch::IntegrationTest
117
- # Register by name, consider Acceptance to be deprecated
118
- register_spec_type(/(Integration|Acceptance)( ?Test)?\z/i, self)
95
+ # Use AD::IntegrationTest for the base class when describing a controller
96
+ register_spec_type(self) do |desc|
97
+ Class === desc && desc < ActionController::Metal
98
+ end
99
+ # Register by name, either Integration or Controller
100
+ register_spec_type(/(Integration|Controller)( ?Test)?\z/i, self)
119
101
  register_spec_type(self) do |desc, *addl|
120
- addl.include? :integration
102
+ addl.include?(:integration) ||
103
+ addl.include?(:controller)
121
104
  end
122
105
  end
123
106
 
@@ -37,567 +37,6 @@ class ActiveSupport::TestCase
37
37
 
38
38
  end
39
39
 
40
- class ActionController::TestCase
41
- # Asserts that the response is one of the following types:
42
- #
43
- # * <tt>:success</tt> - Status code was in the 200-299 range
44
- # * <tt>:redirect</tt> - Status code was in the 300-399 range
45
- # * <tt>:missing</tt> - Status code was 404
46
- # * <tt>:error</tt> - Status code was in the 500-599 range
47
- #
48
- # You can also pass an explicit status number like <tt>assert_response(501)</tt>
49
- # or its symbolic equivalent <tt>assert_response(:not_implemented)</tt>.
50
- # See Rack::Utils::SYMBOL_TO_STATUS_CODE for a full list.
51
- #
52
- # # assert that the response was a redirection
53
- # assert_response :redirect
54
- #
55
- # # assert that the response code was status code 401 (unauthorized)
56
- # assert_response 401
57
- #
58
- # See also Minitest::Rails::Expectations#must_respond_with
59
- #
60
- # :method: assert_response
61
- # :call-seq: assert_response(type, message = nil)
62
-
63
- ##
64
- # Assert that the redirection options passed in match those of the redirect called in the latest action.
65
- # This match can be partial, such that <tt>assert_redirected_to(controller: "weblog")</tt> will also
66
- # match the redirection of <tt>redirect_to(controller: "weblog", action: "show")</tt> and so on.
67
- #
68
- # # assert that the redirection was to the "index" action on the WeblogController
69
- # assert_redirected_to controller: "weblog", action: "index"
70
- #
71
- # # assert that the redirection was to the named route login_url
72
- # assert_redirected_to login_url
73
- #
74
- # # assert that the redirection was to the url for @customer
75
- # assert_redirected_to @customer
76
- #
77
- # # asserts that the redirection matches the regular expression
78
- # assert_redirected_to %r(\Ahttp://example.org)
79
- #
80
- # See also Minitest::Rails::Expectations#must_redirect_to
81
- #
82
- # :method: assert_redirected_to
83
- # :call-seq: assert_redirected_to(options = {}, message=nil)
84
-
85
- ##
86
- # Asserts that the request was rendered with the appropriate template file or partials.
87
- #
88
- # # assert that the "new" view template was rendered
89
- # assert_template "new"
90
- #
91
- # # assert that the exact template "admin/posts/new" was rendered
92
- # assert_template %r{\Aadmin/posts/new\Z}
93
- #
94
- # # assert that the layout 'admin' was rendered
95
- # assert_template layout: 'admin'
96
- # assert_template layout: 'layouts/admin'
97
- # assert_template layout: :admin
98
- #
99
- # # assert that no layout was rendered
100
- # assert_template layout: nil
101
- # assert_template layout: false
102
- #
103
- # # assert that the "_customer" partial was rendered twice
104
- # assert_template partial: '_customer', count: 2
105
- #
106
- # # assert that no partials were rendered
107
- # assert_template partial: false
108
- #
109
- # In a view test case, you can also assert that specific locals are passed
110
- # to partials:
111
- #
112
- # # assert that the "_customer" partial was rendered with a specific object
113
- # assert_template partial: '_customer', locals: { customer: @customer }
114
- #
115
- # See also Minitest::Rails::Expectations#must_render_template
116
- #
117
- # :method: assert_template
118
- # :call-seq: assert_template(options = {}, message = nil)
119
-
120
- ##
121
- # Asserts that the provided options can be used to generate the provided path. This is the inverse of +assert_recognizes+.
122
- # The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in
123
- # a query string. The +message+ parameter allows you to specify a custom error message for assertion failures.
124
- #
125
- # The +defaults+ parameter is unused.
126
- #
127
- # # Asserts that the default action is generated for a route with no action
128
- # assert_generates "/items", controller: "items", action: "index"
129
- #
130
- # # Tests that the list action is properly routed
131
- # assert_generates "/items/list", controller: "items", action: "list"
132
- #
133
- # # Tests the generation of a route with a parameter
134
- # assert_generates "/items/list/1", { controller: "items", action: "list", id: "1" }
135
- #
136
- # # Asserts that the generated route gives us our custom route
137
- # assert_generates "changesets/12", { controller: 'scm', action: 'show_diff', revision: "12" }
138
- #
139
- # See also Minitest::Rails::Expectations#must_route_to
140
- #
141
- # :method: assert_generates
142
- # :call-seq: assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
143
-
144
- ##
145
- # Asserts that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash)
146
- # match +path+. Basically, it asserts that \Rails recognizes the route given by +expected_options+.
147
- #
148
- # Pass a hash in the second argument (+path+) to specify the request method. This is useful for routes
149
- # requiring a specific HTTP method. The hash should contain a :path with the incoming request path
150
- # and a :method containing the required HTTP verb.
151
- #
152
- # # assert that POSTing to /items will call the create action on ItemsController
153
- # assert_recognizes({controller: 'items', action: 'create'}, {path: 'items', method: :post})
154
- #
155
- # You can also pass in +extras+ with a hash containing URL parameters that would normally be in the query string. This can be used
156
- # to assert that values in the query string string will end up in the params hash correctly. To test query strings you must use the
157
- # extras argument, appending the query string on the path directly will not work. For example:
158
- #
159
- # # assert that a path of '/items/list/1?view=print' returns the correct options
160
- # assert_recognizes({controller: 'items', action: 'list', id: '1', view: 'print'}, 'items/list/1', { view: "print" })
161
- #
162
- # The +message+ parameter allows you to pass in an error message that is displayed upon failure.
163
- #
164
- # # Check the default route (i.e., the index action)
165
- # assert_recognizes({controller: 'items', action: 'index'}, 'items')
166
- #
167
- # # Test a specific action
168
- # assert_recognizes({controller: 'items', action: 'list'}, 'items/list')
169
- #
170
- # # Test an action with a parameter
171
- # assert_recognizes({controller: 'items', action: 'destroy', id: '1'}, 'items/destroy/1')
172
- #
173
- # # Test a custom route
174
- # assert_recognizes({controller: 'items', action: 'show', id: '1'}, 'view/item1')
175
- #
176
- # See also Minitest::Rails::Expectations#must_route_from
177
- #
178
- # :method: assert_recognizes
179
- # :call-seq: assert_recognizes(expected_options, path, extras={}, msg=nil)
180
-
181
- ##
182
- # Asserts that path and options match both ways; in other words, it verifies that <tt>path</tt> generates
183
- # <tt>options</tt> and then that <tt>options</tt> generates <tt>path</tt>. This essentially combines +assert_recognizes+
184
- # and +assert_generates+ into one step.
185
- #
186
- # The +extras+ hash allows you to specify options that would normally be provided as a query string to the action. The
187
- # +message+ parameter allows you to specify a custom error message to display upon failure.
188
- #
189
- # # Assert a basic route: a controller with the default action (index)
190
- # assert_routing '/home', controller: 'home', action: 'index'
191
- #
192
- # # Test a route generated with a specific controller, action, and parameter (id)
193
- # assert_routing '/entries/show/23', controller: 'entries', action: 'show', id: 23
194
- #
195
- # # Assert a basic route (controller + default action), with an error message if it fails
196
- # assert_routing '/store', { controller: 'store', action: 'index' }, {}, {}, 'Route for store index not generated properly'
197
- #
198
- # # Tests a route, providing a defaults hash
199
- # assert_routing 'controller/action/9', {id: "9", item: "square"}, {controller: "controller", action: "action"}, {}, {item: "square"}
200
- #
201
- # # Tests a route with a HTTP method
202
- # assert_routing({ method: 'put', path: '/product/321' }, { controller: "product", action: "update", id: "321" })
203
- #
204
- # See also Minitest::Rails::Expectations#must_route
205
- #
206
- # :method: assert_routing
207
- # :call-seq: assert_routing(path, options, defaults={}, extras={}, message=nil)
208
-
209
- # An assertion that selects elements and makes one or more equality tests.
210
- #
211
- # If the first argument is an element, selects all matching elements
212
- # starting from (and including) that element and all its children in
213
- # depth-first order.
214
- #
215
- # If no element if specified, calling +assert_select+ selects from the
216
- # response HTML unless +assert_select+ is called from within an +assert_select+ block.
217
- #
218
- # When called with a block +assert_select+ passes an array of selected elements
219
- # to the block. Calling +assert_select+ from the block, with no element specified,
220
- # runs the assertion on the complete set of elements selected by the enclosing assertion.
221
- # Alternatively the array may be iterated through so that +assert_select+ can be called
222
- # separately for each element.
223
- #
224
- #
225
- # ==== Example
226
- # If the response contains two ordered lists, each with four list elements then:
227
- # assert_select "ol" do |elements|
228
- # elements.each do |element|
229
- # assert_select element, "li", 4
230
- # end
231
- # end
232
- #
233
- # will pass, as will:
234
- # assert_select "ol" do
235
- # assert_select "li", 8
236
- # end
237
- #
238
- # The selector may be a CSS selector expression (String), an expression
239
- # with substitution values, or an HTML::Selector object.
240
- #
241
- # === Equality Tests
242
- #
243
- # The equality test may be one of the following:
244
- # * <tt>true</tt> - Assertion is true if at least one element selected.
245
- # * <tt>false</tt> - Assertion is true if no element selected.
246
- # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
247
- # one element matches the string or regular expression.
248
- # * <tt>Integer</tt> - Assertion is true if exactly that number of
249
- # elements are selected.
250
- # * <tt>Range</tt> - Assertion is true if the number of selected
251
- # elements fit the range.
252
- # If no equality test specified, the assertion is true if at least one
253
- # element selected.
254
- #
255
- # To perform more than one equality tests, use a hash with the following keys:
256
- # * <tt>:text</tt> - Narrow the selection to elements that have this text
257
- # value (string or regexp).
258
- # * <tt>:html</tt> - Narrow the selection to elements that have this HTML
259
- # content (string or regexp).
260
- # * <tt>:count</tt> - Assertion is true if the number of selected elements
261
- # is equal to this value.
262
- # * <tt>:minimum</tt> - Assertion is true if the number of selected
263
- # elements is at least this value.
264
- # * <tt>:maximum</tt> - Assertion is true if the number of selected
265
- # elements is at most this value.
266
- #
267
- # If the method is called with a block, once all equality tests are
268
- # evaluated the block is called with an array of all matched elements.
269
- #
270
- # # At least one form element
271
- # assert_select "form"
272
- #
273
- # # Form element includes four input fields
274
- # assert_select "form input", 4
275
- #
276
- # # Page title is "Welcome"
277
- # assert_select "title", "Welcome"
278
- #
279
- # # Page title is "Welcome" and there is only one title element
280
- # assert_select "title", {count: 1, text: "Welcome"},
281
- # "Wrong title or more than one title element"
282
- #
283
- # # Page contains no forms
284
- # assert_select "form", false, "This page must contain no forms"
285
- #
286
- # # Test the content and style
287
- # assert_select "body div.header ul.menu"
288
- #
289
- # # Use substitution values
290
- # assert_select "ol>li#?", /item-\d+/
291
- #
292
- # # All input fields in the form have a name
293
- # assert_select "form input" do
294
- # assert_select "[name=?]", /.+/ # Not empty
295
- # end
296
- #
297
- # See also Minitest::Rails::Expectations#must_select
298
- #
299
- # :method: assert_select
300
- # :call-seq: assert_select(*args, &block)
301
-
302
- # Extracts the body of an email and runs nested assertions on it.
303
- #
304
- # You must enable deliveries for this assertion to work, use:
305
- # ActionMailer::Base.perform_deliveries = true
306
- #
307
- # assert_select_email do
308
- # assert_select "h1", "Email alert"
309
- # end
310
- #
311
- # assert_select_email do
312
- # items = assert_select "ol>li"
313
- # items.each do
314
- # # Work with items here...
315
- # end
316
- # end
317
- #
318
- # See also Minitest::Rails::Expectations#must_select_email
319
- #
320
- # :method: assert_select_email
321
- # :call-seq: assert_select_email(&block)
322
-
323
- # Extracts the content of an element, treats it as encoded HTML and runs
324
- # nested assertion on it.
325
- #
326
- # You typically call this method within another assertion to operate on
327
- # all currently selected elements. You can also pass an element or array
328
- # of elements.
329
- #
330
- # The content of each element is un-encoded, and wrapped in the root
331
- # element +encoded+. It then calls the block with all un-encoded elements.
332
- #
333
- # # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
334
- # assert_select "feed[xmlns='http://www.w3.org/2005/Atom']" do
335
- # # Select each entry item and then the title item
336
- # assert_select "entry>title" do
337
- # # Run assertions on the encoded title elements
338
- # assert_select_encoded do
339
- # assert_select "b"
340
- # end
341
- # end
342
- # end
343
- #
344
- #
345
- # # Selects all paragraph tags from within the description of an RSS feed
346
- # assert_select "rss[version=2.0]" do
347
- # # Select description element of each feed item.
348
- # assert_select "channel>item>description" do
349
- # # Run assertions on the encoded elements.
350
- # assert_select_encoded do
351
- # assert_select "p"
352
- # end
353
- # end
354
- # end
355
- #
356
- # See also Minitest::Rails::Expectations#must_select_encoded
357
- #
358
- # :method: assert_select_encoded
359
- # :call-seq: assert_select_encoded(element = nil, &block)
360
-
361
- ##
362
- # Checks that two HTML strings are equivalent. That they contain the same elements and attributes with the associated values.
363
- #
364
- # assert_dom_equal '<a href="http://www.example.com">Apples</a>',
365
- # link_to("Apples", "http://www.example.com")
366
- #
367
- # See also Minitest::Rails::Expectations#must_dom_equal
368
- #
369
- # :method: assert_dom_equal
370
- # :call-seq: assert_dom_equal(expected, actual, message = nil)
371
-
372
- ##
373
- # Checks that two HTML strings are not equivalent. That they do not contain the same elements and attributes with the associated values.
374
- #
375
- # assert_dom_not_equal '<a href="http://www.example.com">Oranges</a>',
376
- # link_to("Apples", "http://www.example.com")
377
- #
378
- # See also Minitest::Rails::Expectations#wont_dom_equal
379
- #
380
- # :method: assert_dom_not_equal
381
- # :call-seq: assert_dom_not_equal(expected, actual, message = nil)
382
-
383
- ##
384
- # Checks that two HTML strings are not equivalent. That they do not contain the same elements and attributes with the associated values.
385
- #
386
- # refute_dom_equal '<a href="http://www.example.com">Oranges</a>',
387
- # link_to("Apples", "http://www.example.com")
388
- #
389
- # See also Minitest::Rails::Expectations#wont_dom_equal
390
- #
391
- # :method: assert_dom_equal
392
- # :call-seq: assert_dom_equal(expected, actual, message = nil)
393
- alias :refute_dom_equal :assert_dom_not_equal
394
-
395
- ##
396
- # Asserts that there is a tag/node/element in the body of the response
397
- # that meets all of the given conditions. The +conditions+ parameter must
398
- # be a hash of any of the following keys (all are optional):
399
- #
400
- # * <tt>:tag</tt>: the node type must match the corresponding value
401
- # * <tt>:attributes</tt>: a hash. The node's attributes must match the
402
- # corresponding values in the hash.
403
- # * <tt>:parent</tt>: a hash. The node's parent must match the
404
- # corresponding hash.
405
- # * <tt>:child</tt>: a hash. At least one of the node's immediate children
406
- # must meet the criteria described by the hash.
407
- # * <tt>:ancestor</tt>: a hash. At least one of the node's ancestors must
408
- # meet the criteria described by the hash.
409
- # * <tt>:descendant</tt>: a hash. At least one of the node's descendants
410
- # must meet the criteria described by the hash.
411
- # * <tt>:sibling</tt>: a hash. At least one of the node's siblings must
412
- # meet the criteria described by the hash.
413
- # * <tt>:after</tt>: a hash. The node must be after any sibling meeting
414
- # the criteria described by the hash, and at least one sibling must match.
415
- # * <tt>:before</tt>: a hash. The node must be before any sibling meeting
416
- # the criteria described by the hash, and at least one sibling must match.
417
- # * <tt>:children</tt>: a hash, for counting children of a node. Accepts
418
- # the keys:
419
- # * <tt>:count</tt>: either a number or a range which must equal (or
420
- # include) the number of children that match.
421
- # * <tt>:less_than</tt>: the number of matching children must be less
422
- # than this number.
423
- # * <tt>:greater_than</tt>: the number of matching children must be
424
- # greater than this number.
425
- # * <tt>:only</tt>: another hash consisting of the keys to use
426
- # to match on the children, and only matching children will be
427
- # counted.
428
- # * <tt>:content</tt>: the textual content of the node must match the
429
- # given value. This will not match HTML tags in the body of a
430
- # tag--only text.
431
- #
432
- # Conditions are matched using the following algorithm:
433
- #
434
- # * if the condition is a string, it must be a substring of the value.
435
- # * if the condition is a regexp, it must match the value.
436
- # * if the condition is a number, the value must match number.to_s.
437
- # * if the condition is +true+, the value must not be +nil+.
438
- # * if the condition is +false+ or +nil+, the value must be +nil+.
439
- #
440
- # # Assert that there is a "span" tag
441
- # assert_tag tag: "span"
442
- #
443
- # # Assert that there is a "span" tag with id="x"
444
- # assert_tag tag: "span", attributes: { id: "x" }
445
- #
446
- # # Assert that there is a "span" tag using the short-hand
447
- # assert_tag :span
448
- #
449
- # # Assert that there is a "span" tag with id="x" using the short-hand
450
- # assert_tag :span, attributes: { id: "x" }
451
- #
452
- # # Assert that there is a "span" inside of a "div"
453
- # assert_tag tag: "span", parent: { tag: "div" }
454
- #
455
- # # Assert that there is a "span" somewhere inside a table
456
- # assert_tag tag: "span", ancestor: { tag: "table" }
457
- #
458
- # # Assert that there is a "span" with at least one "em" child
459
- # assert_tag tag: "span", child: { tag: "em" }
460
- #
461
- # # Assert that there is a "span" containing a (possibly nested)
462
- # # "strong" tag.
463
- # assert_tag tag: "span", descendant: { tag: "strong" }
464
- #
465
- # # Assert that there is a "span" containing between 2 and 4 "em" tags
466
- # # as immediate children
467
- # assert_tag tag: "span",
468
- # children: { count: 2..4, only: { tag: "em" } }
469
- #
470
- # # Get funky: assert that there is a "div", with an "ul" ancestor
471
- # # and an "li" parent (with "class" = "enum"), and containing a
472
- # # "span" descendant that contains text matching /hello world/
473
- # assert_tag tag: "div",
474
- # ancestor: { tag: "ul" },
475
- # parent: { tag: "li",
476
- # attributes: { class: "enum" } },
477
- # descendant: { tag: "span",
478
- # child: /hello world/ }
479
- #
480
- # <b>Please note</b>: +assert_tag+ and +assert_no_tag+ only work
481
- # with well-formed XHTML. They recognize a few tags as implicitly self-closing
482
- # (like br and hr and such) but will not work correctly with tags
483
- # that allow optional closing tags (p, li, td). <em>You must explicitly
484
- # close all of your tags to use these assertions.</em>
485
- #
486
- # See also Minitest::Rails::Expectations#must_have_tag
487
- #
488
- # :method: assert_tag
489
- # :call-seq: assert_tag(*opts)
490
-
491
- ##
492
- # Identical to +assert_tag+, but asserts that a matching tag does _not_
493
- # exist. (See +assert_tag+ for a full discussion of the syntax.)
494
- #
495
- # # Assert that there is not a "div" containing a "p"
496
- # assert_no_tag tag: "div", descendant: { tag: "p" }
497
- #
498
- # # Assert that an unordered list is empty
499
- # assert_no_tag tag: "ul", descendant: { tag: "li" }
500
- #
501
- # # Assert that there is not a "p" tag with between 1 to 3 "img" tags
502
- # # as immediate children
503
- # assert_no_tag tag: "p",
504
- # children: { count: 1..3, only: { tag: "img" } }
505
- #
506
- # See also Minitest::Rails::Expectations#wont_have_tag
507
- #
508
- # :method: assert_no_tag
509
- # :call-seq: assert_no_tag(*opts)
510
-
511
- ##
512
- # Identical to +assert_tag+, but asserts that a matching tag does _not_
513
- # exist. (See +assert_tag+ for a full discussion of the syntax.)
514
- #
515
- # # Assert that there is not a "div" containing a "p"
516
- # assert_no_tag tag: "div", descendant: { tag: "p" }
517
- #
518
- # # Assert that an unordered list is empty
519
- # assert_no_tag tag: "ul", descendant: { tag: "li" }
520
- #
521
- # # Assert that there is not a "p" tag with between 1 to 3 "img" tags
522
- # # as immediate children
523
- # assert_no_tag tag: "p",
524
- # children: { count: 1..3, only: { tag: "img" } }
525
- #
526
- # See also Minitest::Rails::Expectations#wont_have_tag
527
- #
528
- # :method: refute_tag
529
- # :call-seq: refute_tag(*opts)
530
- alias :refute_tag :assert_no_tag
531
-
532
- ##
533
- # Simulate a GET request with the given parameters.
534
- #
535
- # - +action+: The controller action to call.
536
- # - +params+: The hash with HTTP parameters that you want to pass. This may be
537
- # +nil+.
538
- # - +body+: The request body with a string that is appropriately encoded
539
- # (<tt>application/x-www-form-urlencoded</tt> or
540
- # <tt>multipart/form-data</tt>).
541
- # - +session+: A hash of parameters to store in the session. This may be
542
- # +nil+.
543
- # - +flash+: A hash of parameters to store in the flash. This may be +nil+.
544
- #
545
- # You can also simulate POST, PATCH, PUT, DELETE, and HEAD requests with
546
- # +post+, +patch+, +put+, +delete+, and +head+.
547
- # Example sending parameters, session and setting a flash message:
548
- #
549
- # get :show,
550
- # params: { id: 7 },
551
- # session: { user_id: 1 },
552
- # flash: { notice: 'This is a flash message' }
553
- #
554
- # Note that the request method is not verified. The different methods are
555
- # available to make the tests more expressive.
556
- #
557
- # :method: get
558
- # :call-seq: get(action, *args)
559
-
560
- ##
561
- # Simulate a POST request with the given parameters and set/volley the
562
- # response.
563
- # See +get+ for more details.
564
- #
565
- # :method: post
566
- # :call-seq: post(action, *args)
567
-
568
- ##
569
- # Simulate a PATCH request with the given parameters and set/volley the
570
- # response.
571
- # See +get+ for more details.
572
- #
573
- # :method: patch
574
- # :call-seq: patch(action, *args)
575
-
576
- ##
577
- # Simulate a PUT request with the given parameters and set/volley the
578
- # response.
579
- # See +get+ for more details.
580
- #
581
- # :method: put
582
- # :call-seq: put(action, *args)
583
-
584
- ##
585
- # Simulate a DELETE request with the given parameters and set/volley the
586
- # response.
587
- # See +get+ for more details.
588
- #
589
- # :method: delete
590
- # :call-seq: delete(action, *args)
591
-
592
- ##
593
- # Simulate a HEAD request with the given parameters and set/volley the
594
- # response.
595
- # See +get+ for more details.
596
- #
597
- # :method: head
598
- # :call-seq: head(action, *args)
599
- end
600
-
601
40
  class ActionView::TestCase
602
41
  ##
603
42
  # Checks the numeric difference between the return value of an expression as a result of what is evaluated.