rspec-rails 2.14.2 → 3.9.1

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 (99) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.document +1 -1
  4. data/.yardopts +4 -2
  5. data/Capybara.md +2 -4
  6. data/Changelog.md +592 -34
  7. data/{License.txt → LICENSE.md} +5 -2
  8. data/README.md +290 -369
  9. data/lib/generators/rspec/controller/controller_generator.rb +1 -0
  10. data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
  11. data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
  12. data/lib/generators/rspec/feature/feature_generator.rb +29 -0
  13. data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
  14. data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
  15. data/lib/generators/rspec/generators/generator_generator.rb +24 -0
  16. data/lib/generators/rspec/generators/templates/generator_spec.rb +6 -0
  17. data/lib/generators/rspec/helper/helper_generator.rb +1 -0
  18. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  19. data/lib/generators/rspec/install/install_generator.rb +44 -5
  20. data/lib/generators/rspec/install/templates/spec/rails_helper.rb +78 -0
  21. data/lib/generators/rspec/integration/integration_generator.rb +8 -13
  22. data/lib/generators/rspec/integration/templates/request_spec.rb +4 -9
  23. data/lib/generators/rspec/job/job_generator.rb +12 -0
  24. data/lib/generators/rspec/job/templates/job_spec.rb.erb +7 -0
  25. data/lib/generators/rspec/mailer/mailer_generator.rb +7 -0
  26. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +7 -7
  27. data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
  28. data/lib/generators/rspec/model/model_generator.rb +19 -5
  29. data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
  30. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  31. data/lib/generators/rspec/observer/observer_generator.rb +1 -0
  32. data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -2
  33. data/lib/generators/rspec/request/request_generator.rb +10 -0
  34. data/lib/generators/rspec/scaffold/scaffold_generator.rb +68 -138
  35. data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +165 -0
  36. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +98 -73
  37. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +9 -13
  38. data/lib/generators/rspec/scaffold/templates/index_spec.rb +3 -10
  39. data/lib/generators/rspec/scaffold/templates/new_spec.rb +10 -14
  40. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +21 -12
  41. data/lib/generators/rspec/scaffold/templates/show_spec.rb +4 -11
  42. data/lib/generators/rspec/system/system_generator.rb +26 -0
  43. data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
  44. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  45. data/lib/generators/rspec/view/view_generator.rb +1 -0
  46. data/lib/generators/rspec.rb +20 -6
  47. data/lib/rspec/rails/active_record.rb +25 -0
  48. data/lib/rspec/rails/adapters.rb +104 -37
  49. data/lib/rspec/rails/configuration.rb +148 -0
  50. data/lib/rspec/rails/example/controller_example_group.rb +188 -138
  51. data/lib/rspec/rails/example/feature_example_group.rb +63 -20
  52. data/lib/rspec/rails/example/helper_example_group.rb +35 -26
  53. data/lib/rspec/rails/example/job_example_group.rb +23 -0
  54. data/lib/rspec/rails/example/mailer_example_group.rb +30 -14
  55. data/lib/rspec/rails/example/model_example_group.rb +8 -7
  56. data/lib/rspec/rails/example/rails_example_group.rb +3 -1
  57. data/lib/rspec/rails/example/request_example_group.rb +23 -16
  58. data/lib/rspec/rails/example/routing_example_group.rb +49 -40
  59. data/lib/rspec/rails/example/system_example_group.rb +108 -0
  60. data/lib/rspec/rails/example/view_example_group.rb +168 -135
  61. data/lib/rspec/rails/example.rb +2 -33
  62. data/lib/rspec/rails/extensions/active_record/proxy.rb +0 -1
  63. data/lib/rspec/rails/extensions.rb +0 -1
  64. data/lib/rspec/rails/feature_check.rb +64 -0
  65. data/lib/rspec/rails/file_fixture_support.rb +17 -0
  66. data/lib/rspec/rails/fixture_file_upload_support.rb +40 -0
  67. data/lib/rspec/rails/fixture_support.rb +32 -13
  68. data/lib/rspec/rails/matchers/active_job.rb +317 -0
  69. data/lib/rspec/rails/matchers/base_matcher.rb +184 -0
  70. data/lib/rspec/rails/matchers/be_a_new.rb +69 -62
  71. data/lib/rspec/rails/matchers/be_new_record.rb +24 -21
  72. data/lib/rspec/rails/matchers/be_valid.rb +42 -33
  73. data/lib/rspec/rails/matchers/have_enqueued_mail.rb +174 -0
  74. data/lib/rspec/rails/matchers/have_http_status.rb +381 -0
  75. data/lib/rspec/rails/matchers/have_rendered.rb +54 -31
  76. data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
  77. data/lib/rspec/rails/matchers/relation_match_array.rb +1 -1
  78. data/lib/rspec/rails/matchers/routing_matchers.rb +107 -93
  79. data/lib/rspec/rails/matchers.rb +13 -14
  80. data/lib/rspec/rails/tasks/rspec.rake +1 -1
  81. data/lib/rspec/rails/vendor/capybara.rb +10 -4
  82. data/lib/rspec/rails/version.rb +3 -1
  83. data/lib/rspec/rails/view_assigns.rb +18 -18
  84. data/lib/rspec/rails/view_path_builder.rb +29 -0
  85. data/lib/rspec/rails/view_rendering.rb +89 -63
  86. data/lib/rspec/rails/view_spec_methods.rb +56 -0
  87. data/lib/rspec/rails.rb +10 -10
  88. data/lib/rspec-rails.rb +66 -1
  89. data.tar.gz.sig +0 -0
  90. metadata +92 -77
  91. metadata.gz.sig +0 -0
  92. data/lib/autotest/rails_rspec2.rb +0 -85
  93. data/lib/generators/rspec/install/templates/.rspec +0 -1
  94. data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -49
  95. data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
  96. data/lib/rspec/rails/matchers/have_extension.rb +0 -36
  97. data/lib/rspec/rails/mocks.rb +0 -274
  98. data/lib/rspec/rails/module_inclusion.rb +0 -19
  99. data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -1,9 +1,9 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/edit" do
4
+ RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
6
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
8
  :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
@@ -13,19 +13,15 @@ describe "<%= ns_table_name %>/edit" do
13
13
  it "renders the edit <%= ns_file_name %> form" do
14
14
  render
15
15
 
16
- <% if webrat? -%>
17
- rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= ns_file_name %>), :method => "post") do |form|
18
- <% for attribute in output_attributes -%>
19
- form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
20
- <% end -%>
21
- end
22
- <% else -%>
23
- # Run the generator again with the --webrat flag if you want to use webrat matchers
24
16
  assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= ns_file_name %>), "post" do
25
17
  <% for attribute in output_attributes -%>
26
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>[name=?]", "<%= ns_file_name %>[<%= attribute.name %>]"
18
+ <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
19
+ <% if Rails.version.to_f >= 5.1 -%>
20
+ assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
21
+ <% else -%>
22
+ assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
27
23
  <% end -%>
28
- end
29
24
  <% end -%>
25
+ end
30
26
  end
31
27
  end
@@ -1,11 +1,11 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/index" do
4
+ RSpec.describe "<%= ns_table_name %>/index", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
6
  assign(:<%= table_name %>, [
7
7
  <% [1,2].each_with_index do |id, model_index| -%>
8
- stub_model(<%= class_name %><%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : ',' %>
8
+ <%= class_name %>.create!(<%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : '' %>
9
9
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
10
10
  :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
11
11
  <% end -%>
@@ -18,15 +18,8 @@ describe "<%= ns_table_name %>/index" do
18
18
 
19
19
  it "renders a list of <%= ns_table_name %>" do
20
20
  render
21
- <% unless webrat? -%>
22
- # Run the generator again with the --webrat flag if you want to use webrat matchers
23
- <% end -%>
24
21
  <% for attribute in output_attributes -%>
25
- <% if webrat? -%>
26
- rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
27
- <% else -%>
28
22
  assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
29
- <% end -%>
30
23
  <% end -%>
31
24
  end
32
25
  end
@@ -1,30 +1,26 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/new" do
4
+ RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
- assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
6
+ assign(:<%= ns_file_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
8
  :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
10
- <%= !output_attributes.empty? ? " ).as_new_record)\n end" : " end" %>
10
+ <%= !output_attributes.empty? ? " ))\n end" : " end" %>
11
11
 
12
12
  it "renders new <%= ns_file_name %> form" do
13
13
  render
14
14
 
15
- <% if webrat? -%>
16
- rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
17
- <% for attribute in output_attributes -%>
18
- form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
19
- <% end -%>
20
- end
21
- <% else -%>
22
- # Run the generator again with the --webrat flag if you want to use webrat matchers
23
15
  assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do
24
16
  <% for attribute in output_attributes -%>
25
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>[name=?]", "<%= ns_file_name %>[<%= attribute.name %>]"
17
+ <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name %>
18
+ <% if Rails.version.to_f >= 5.1 -%>
19
+ assert_select "<%= attribute.input_type -%>[name=?]", "<%= ns_file_name %>[<%= name %>]"
20
+ <% else -%>
21
+ assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
26
22
  <% end -%>
27
- end
28
23
  <% end -%>
24
+ end
29
25
  end
30
26
  end
@@ -1,39 +1,48 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- describe <%= controller_class_name %>Controller do
4
+ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:routing) %> do
5
5
  describe "routing" do
6
-
7
6
  <% unless options[:singleton] -%>
8
7
  it "routes to #index" do
9
- get("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#index")
8
+ expect(:get => "/<%= ns_table_name %>").to route_to("<%= ns_table_name %>#index")
10
9
  end
11
10
 
12
11
  <% end -%>
12
+ <% unless options[:api] -%>
13
13
  it "routes to #new" do
14
- get("/<%= ns_table_name %>/new").should route_to("<%= ns_table_name %>#new")
14
+ expect(:get => "/<%= ns_table_name %>/new").to route_to("<%= ns_table_name %>#new")
15
15
  end
16
16
 
17
+ <% end -%>
17
18
  it "routes to #show" do
18
- get("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#show", :id => "1")
19
+ expect(:get => "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#show", :id => "1")
19
20
  end
20
21
 
22
+ <% unless options[:api] -%>
21
23
  it "routes to #edit" do
22
- get("/<%= ns_table_name %>/1/edit").should route_to("<%= ns_table_name %>#edit", :id => "1")
24
+ expect(:get => "/<%= ns_table_name %>/1/edit").to route_to("<%= ns_table_name %>#edit", :id => "1")
23
25
  end
24
26
 
27
+ <% end -%>
28
+
25
29
  it "routes to #create" do
26
- post("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#create")
30
+ expect(:post => "/<%= ns_table_name %>").to route_to("<%= ns_table_name %>#create")
27
31
  end
28
32
 
29
- it "routes to #update" do
30
- put("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#update", :id => "1")
33
+ it "routes to #update via PUT" do
34
+ expect(:put => "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#update", :id => "1")
31
35
  end
32
36
 
33
- it "routes to #destroy" do
34
- delete("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#destroy", :id => "1")
37
+ <% if Rails::VERSION::STRING > '4' -%>
38
+ it "routes to #update via PATCH" do
39
+ expect(:patch => "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#update", :id => "1")
35
40
  end
36
41
 
42
+ <% end -%>
43
+ it "routes to #destroy" do
44
+ expect(:delete => "/<%= ns_table_name %>/1").to route_to("<%= ns_table_name %>#destroy", :id => "1")
45
+ end
37
46
  end
38
47
  end
39
48
  <% end -%>
@@ -1,9 +1,9 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/show" do
4
+ RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
5
5
  before(:each) do
6
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
6
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
8
  :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
9
  <% end -%>
@@ -14,15 +14,8 @@ describe "<%= ns_table_name %>/show" do
14
14
 
15
15
  it "renders attributes in <p>" do
16
16
  render
17
- <% unless webrat? -%>
18
- # Run the generator again with the --webrat flag if you want to use webrat matchers
19
- <% end -%>
20
17
  <% for attribute in output_attributes -%>
21
- <% if webrat? -%>
22
- rendered.should contain(<%= value_for(attribute) %>.to_s)
23
- <% else -%>
24
- rendered.should match(/<%= eval(value_for(attribute)) %>/)
25
- <% end -%>
18
+ expect(rendered).to match(/<%= raw_value_for(attribute) %>/)
26
19
  <% end -%>
27
20
  end
28
21
  end
@@ -0,0 +1,26 @@
1
+ require 'generators/rspec'
2
+
3
+ if ::Rails::VERSION::STRING >= '5.1'
4
+ module Rspec
5
+ module Generators
6
+ # @private
7
+ class SystemGenerator < Base
8
+ class_option :system_specs, :type => :boolean, :default => true, :desc => "Generate system specs"
9
+
10
+ def generate_system_spec
11
+ return unless options[:system_specs]
12
+
13
+ template template_name, File.join('spec/system', class_path, filename)
14
+ end
15
+
16
+ def template_name
17
+ 'system_spec.rb'
18
+ end
19
+
20
+ def filename
21
+ "#{table_name}_spec.rb"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe "<%= class_name.pluralize %>", <%= type_metatag(:system) %> do
4
+ before do
5
+ driven_by(:rack_test)
6
+ end
7
+
8
+ pending "add some scenarios (or delete) #{__FILE__}"
9
+ end
@@ -1,5 +1,5 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
- describe "<%= file_path %>/<%= @action %>" do
3
+ RSpec.describe "<%= file_path %>/<%= @action %>", <%= type_metatag(:view) %> do
4
4
  pending "add some examples to (or delete) #{__FILE__}"
5
5
  end
@@ -2,6 +2,7 @@ require 'generators/rspec'
2
2
 
3
3
  module Rspec
4
4
  module Generators
5
+ # @private
5
6
  class ViewGenerator < Base
6
7
  argument :actions, :type => :array, :default => [], :banner => "action action"
7
8
 
@@ -1,10 +1,22 @@
1
1
  require 'rails/generators/named_base'
2
+ require 'rspec/rails/feature_check'
2
3
 
4
+ # @private
5
+ # Weirdly named generators namespace (should be `RSpec`) for compatability with
6
+ # rails loading.
3
7
  module Rspec
8
+ # @private
4
9
  module Generators
10
+ # @private
5
11
  class Base < ::Rails::Generators::NamedBase
6
- def self.source_root
7
- @_rspec_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'rspec', generator_name, 'templates'))
12
+ include RSpec::Rails::FeatureCheck
13
+
14
+ def self.source_root(path = nil)
15
+ if path
16
+ @_rspec_source_root = path
17
+ else
18
+ @_rspec_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'rspec', generator_name, 'templates'))
19
+ end
8
20
  end
9
21
 
10
22
  if ::Rails::VERSION::STRING < '3.1'
@@ -16,15 +28,17 @@ module Rspec
16
28
  end
17
29
  end
18
30
 
31
+ # @private
19
32
  module Rails
20
33
  module Generators
34
+ # @private
21
35
  class GeneratedAttribute
22
36
  def input_type
23
37
  @input_type ||= if type == :text
24
- "textarea"
25
- else
26
- "input"
27
- end
38
+ "textarea"
39
+ else
40
+ "input"
41
+ end
28
42
  end
29
43
  end
30
44
  end
@@ -0,0 +1,25 @@
1
+ module RSpec
2
+ module Rails
3
+ # Fake class to document RSpec ActiveRecord configuration options. In practice,
4
+ # these are dynamically added to the normal RSpec configuration object.
5
+ class ActiveRecordConfiguration
6
+ # @private
7
+ def self.initialize_activerecord_configuration(config)
8
+ config.before :suite do
9
+ # This allows dynamic columns etc to be used on ActiveRecord models when creating instance_doubles
10
+ if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && defined?(::RSpec::Mocks) && (::RSpec::Mocks.respond_to?(:configuration))
11
+ ::RSpec::Mocks.configuration.when_declaring_verifying_double do |possible_model|
12
+ target = possible_model.target
13
+
14
+ if Class === target && ActiveRecord::Base > target && !target.abstract_class?
15
+ target.define_attribute_methods
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ initialize_activerecord_configuration RSpec.configuration
23
+ end
24
+ end
25
+ end
@@ -1,13 +1,72 @@
1
1
  require 'delegate'
2
2
  require 'active_support'
3
3
  require 'active_support/concern'
4
+ require 'active_support/core_ext/string'
4
5
 
5
6
  module RSpec
6
7
  module Rails
8
+ # @private
9
+ def self.disable_testunit_autorun
10
+ # `Test::Unit::AutoRunner.need_auto_run=` was introduced to the test-unit
11
+ # gem in version 2.4.9. Previous to this version `Test::Unit.run=` was
12
+ # used. The implementation of test-unit included with Ruby has neither
13
+ # method.
14
+ if defined?(Test::Unit::AutoRunner.need_auto_run = ())
15
+ Test::Unit::AutoRunner.need_auto_run = false
16
+ elsif defined?(Test::Unit.run = ())
17
+ Test::Unit.run = false
18
+ end
19
+ end
20
+ private_class_method :disable_testunit_autorun
21
+
7
22
  if ::Rails::VERSION::STRING >= '4.1.0'
8
- gem 'minitest'
23
+ if defined?(Kernel.gem)
24
+ gem 'minitest'
25
+ else
26
+ require 'minitest'
27
+ end
9
28
  require 'minitest/assertions'
29
+ # Constant aliased to either Minitest or TestUnit, depending on what is
30
+ # loaded.
10
31
  Assertions = Minitest::Assertions
32
+ elsif RUBY_VERSION >= '2.2.0'
33
+ # Minitest / TestUnit has been removed from ruby core. However, we are
34
+ # on an old Rails version and must load the appropriate gem
35
+ if ::Rails::VERSION::STRING >= '4.0.0'
36
+ # ActiveSupport 4.0.x has the minitest '~> 4.2' gem as a dependency
37
+ # This gem has no `lib/minitest.rb` file.
38
+ gem 'minitest' if defined?(Kernel.gem)
39
+ require 'minitest/unit'
40
+ Assertions = MiniTest::Assertions
41
+ elsif ::Rails::VERSION::STRING >= '3.2.21'
42
+ # TODO: Change the above check to >= '3.2.22' once it's released
43
+ begin
44
+ # Test::Unit "helpfully" sets up autoload for its `AutoRunner`.
45
+ # While we do not reference it directly, when we load the `TestCase`
46
+ # classes from AS (ActiveSupport), AS kindly references `AutoRunner`
47
+ # for everyone.
48
+ #
49
+ # To handle this we need to pre-emptively load 'test/unit' and make
50
+ # sure the version installed has `AutoRunner` (the 3.x line does to
51
+ # date). If so, we turn the auto runner off.
52
+ require 'test/unit'
53
+ require 'test/unit/assertions'
54
+ disable_testunit_autorun
55
+ rescue LoadError => e
56
+ raise LoadError, <<-ERR.squish, e.backtrace
57
+ Ruby 2.2+ has removed test/unit from the core library. Rails
58
+ requires this as a dependency. Please add test-unit gem to your
59
+ Gemfile: `gem 'test-unit', '~> 3.0'` (#{e.message})"
60
+ ERR
61
+ end
62
+ Assertions = Test::Unit::Assertions
63
+ else
64
+ abort <<-MSG.squish
65
+ Ruby 2.2+ is not supported on Rails #{::Rails::VERSION::STRING}.
66
+ Check the Rails release notes for the appropriate update with
67
+ support.
68
+ MSG
69
+ end
11
70
  else
12
71
  begin
13
72
  require 'test/unit/assertions'
@@ -16,12 +75,15 @@ module RSpec
16
75
  require 'rubysl-test-unit' if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
17
76
  require 'test/unit/assertions'
18
77
  end
78
+ # Turn off test unit's auto runner for those using the gem
79
+ disable_testunit_autorun
80
+ # Constant aliased to either Minitest or TestUnit, depending on what is
81
+ # loaded.
19
82
  Assertions = Test::Unit::Assertions
20
83
  end
21
84
 
22
- # @api private
85
+ # @private
23
86
  class AssertionDelegator < Module
24
- # @api private
25
87
  def initialize(*assertion_modules)
26
88
  assertion_class = Class.new(SimpleDelegator) do
27
89
  include ::RSpec::Rails::Assertions
@@ -30,12 +92,10 @@ module RSpec
30
92
  end
31
93
 
32
94
  super() do
33
- # @api private
34
95
  define_method :build_assertion_instance do
35
96
  assertion_class.new(self)
36
97
  end
37
98
 
38
- # @api private
39
99
  def assertion_instance
40
100
  @assertion_instance ||= build_assertion_instance
41
101
  end
@@ -43,11 +103,9 @@ module RSpec
43
103
  assertion_modules.each do |mod|
44
104
  mod.public_instance_methods.each do |method|
45
105
  next if method == :method_missing || method == "method_missing"
46
- class_eval <<-EOM, __FILE__, __LINE__ + 1
47
- def #{method}(*args, &block)
48
- assertion_instance.send(:#{method}, *args, &block)
49
- end
50
- EOM
106
+ define_method(method.to_sym) do |*args, &block|
107
+ assertion_instance.send(method.to_sym, *args, &block)
108
+ end
51
109
  end
52
110
  end
53
111
  end
@@ -56,7 +114,7 @@ module RSpec
56
114
 
57
115
  # Adapts example groups for `Minitest::Test::LifecycleHooks`
58
116
  #
59
- # @api private
117
+ # @private
60
118
  module MinitestLifecycleAdapter
61
119
  extend ActiveSupport::Concern
62
120
 
@@ -84,29 +142,22 @@ module RSpec
84
142
  end
85
143
  end
86
144
 
87
- # @api private
145
+ # @private
88
146
  module MinitestCounters
89
- # @api private
147
+ attr_writer :assertions
90
148
  def assertions
91
149
  @assertions ||= 0
92
150
  end
93
-
94
- # @api private
95
- def assertions=(assertions)
96
- @assertions = assertions
97
- end
98
151
  end
99
152
 
100
- # @api private
153
+ # @private
101
154
  module SetupAndTeardownAdapter
102
155
  extend ActiveSupport::Concern
103
156
 
104
157
  module ClassMethods
105
- # @api private
106
- #
107
158
  # Wraps `setup` calls from within Rails' testing framework in `before`
108
159
  # hooks.
109
- def setup(*methods)
160
+ def setup(*methods, &block)
110
161
  methods.each do |method|
111
162
  if method.to_s =~ /^setup_(with_controller|fixtures|controller_request_and_response)$/
112
163
  prepend_before { __send__ method }
@@ -114,18 +165,24 @@ module RSpec
114
165
  before { __send__ method }
115
166
  end
116
167
  end
168
+ before(&block) if block
117
169
  end
118
170
 
119
171
  # @api private
120
172
  #
121
173
  # Wraps `teardown` calls from within Rails' testing framework in
122
174
  # `after` hooks.
123
- def teardown(*methods)
175
+ def teardown(*methods, &block)
124
176
  methods.each { |method| after { __send__ method } }
177
+ after(&block) if block
125
178
  end
126
179
  end
127
180
 
128
- # @api private
181
+ def initialize(*args)
182
+ super
183
+ @example = nil
184
+ end
185
+
129
186
  def method_name
130
187
  @example
131
188
  end
@@ -135,36 +192,46 @@ module RSpec
135
192
  module MinitestAssertionAdapter
136
193
  extend ActiveSupport::Concern
137
194
 
195
+ # @private
138
196
  module ClassMethods
139
- # @api private
140
- #
141
197
  # Returns the names of assertion methods that we want to expose to
142
198
  # examples without exposing non-assertion methods in Test::Unit or
143
199
  # Minitest.
144
200
  def assertion_method_names
145
- ::RSpec::Rails::Assertions.public_instance_methods.select{|m| m.to_s =~ /^(assert|flunk|refute)/} +
146
- [:build_message]
201
+ methods = ::RSpec::Rails::Assertions.
202
+ public_instance_methods.
203
+ select do |m|
204
+ m.to_s =~ /^(assert|flunk|refute)/
205
+ end
206
+ methods + test_unit_specific_methods
147
207
  end
148
208
 
149
- # @api private
150
209
  def define_assertion_delegators
151
210
  assertion_method_names.each do |m|
152
- class_eval <<-CODE, __FILE__, __LINE__ + 1
153
- def #{m}(*args, &block)
154
- assertion_delegator.send :#{m}, *args, &block
155
- end
156
- CODE
211
+ define_method(m.to_sym) do |*args, &block|
212
+ assertion_delegator.send(m.to_sym, *args, &block)
213
+ end
214
+ end
215
+ end
216
+
217
+ # Starting on Rails 4, Minitest is the default testing framework so no
218
+ # need to add TestUnit specific methods.
219
+ if ::Rails::VERSION::STRING >= '4.0.0'
220
+ def test_unit_specific_methods
221
+ []
222
+ end
223
+ else
224
+ def test_unit_specific_methods
225
+ [:build_message]
157
226
  end
158
227
  end
159
228
  end
160
229
 
161
- # @api private
162
230
  class AssertionDelegator
163
231
  include ::RSpec::Rails::Assertions
164
232
  include ::RSpec::Rails::MinitestCounters
165
233
  end
166
234
 
167
- # @api private
168
235
  def assertion_delegator
169
236
  @assertion_delegator ||= AssertionDelegator.new
170
237
  end
@@ -177,7 +244,7 @@ module RSpec
177
244
  # Backwards compatibility. It's unlikely that anyone is using this
178
245
  # constant, but we had forgotten to mark it as `@private` earlier
179
246
  #
180
- # @api private
247
+ # @private
181
248
  TestUnitAssertionAdapter = MinitestAssertionAdapter
182
249
  end
183
250
  end