rspec-rails 3.0.0.rc1 → 3.0.0

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 (57) hide show
  1. checksums.yaml +14 -6
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +1 -1
  5. data/Changelog.md +19 -1
  6. data/lib/generators/rspec.rb +6 -0
  7. data/lib/generators/rspec/controller/controller_generator.rb +1 -0
  8. data/lib/generators/rspec/controller/templates/controller_spec.rb +2 -2
  9. data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
  10. data/lib/generators/rspec/feature/feature_generator.rb +1 -0
  11. data/lib/generators/rspec/feature/templates/feature_spec.rb +1 -1
  12. data/lib/generators/rspec/helper/helper_generator.rb +1 -0
  13. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
  14. data/lib/generators/rspec/install/install_generator.rb +24 -4
  15. data/lib/generators/rspec/install/templates/spec/{spec_helper.rb.tt → rails_helper.rb} +4 -17
  16. data/lib/generators/rspec/integration/integration_generator.rb +1 -0
  17. data/lib/generators/rspec/integration/templates/request_spec.rb +2 -2
  18. data/lib/generators/rspec/mailer/mailer_generator.rb +1 -0
  19. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -2
  20. data/lib/generators/rspec/model/model_generator.rb +1 -0
  21. data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
  22. data/lib/generators/rspec/observer/observer_generator.rb +1 -0
  23. data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -2
  24. data/lib/generators/rspec/scaffold/scaffold_generator.rb +8 -33
  25. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +20 -33
  26. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +2 -2
  27. data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
  28. data/lib/generators/rspec/scaffold/templates/new_spec.rb +2 -2
  29. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +2 -2
  30. data/lib/generators/rspec/scaffold/templates/show_spec.rb +3 -3
  31. data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
  32. data/lib/generators/rspec/view/view_generator.rb +1 -0
  33. data/lib/rspec-rails.rb +3 -0
  34. data/lib/rspec/rails/adapters.rb +10 -18
  35. data/lib/rspec/rails/configuration.rb +40 -14
  36. data/lib/rspec/rails/example/controller_example_group.rb +8 -6
  37. data/lib/rspec/rails/example/feature_example_group.rb +5 -0
  38. data/lib/rspec/rails/example/helper_example_group.rb +2 -1
  39. data/lib/rspec/rails/example/mailer_example_group.rb +12 -0
  40. data/lib/rspec/rails/example/model_example_group.rb +2 -0
  41. data/lib/rspec/rails/example/rails_example_group.rb +1 -0
  42. data/lib/rspec/rails/example/request_example_group.rb +2 -0
  43. data/lib/rspec/rails/example/routing_example_group.rb +3 -2
  44. data/lib/rspec/rails/example/view_example_group.rb +5 -4
  45. data/lib/rspec/rails/fixture_support.rb +4 -3
  46. data/lib/rspec/rails/matchers/be_a_new.rb +13 -12
  47. data/lib/rspec/rails/matchers/be_new_record.rb +1 -3
  48. data/lib/rspec/rails/matchers/be_valid.rb +1 -4
  49. data/lib/rspec/rails/matchers/have_rendered.rb +3 -2
  50. data/lib/rspec/rails/matchers/redirect_to.rb +3 -5
  51. data/lib/rspec/rails/matchers/routing_matchers.rb +14 -6
  52. data/lib/rspec/rails/version.rb +3 -1
  53. data/lib/rspec/rails/view_assigns.rb +17 -16
  54. data/lib/rspec/rails/view_rendering.rb +8 -3
  55. metadata +73 -67
  56. metadata.gz.sig +2 -3
  57. data/lib/generators/rspec/install/templates/.rspec +0 -1
@@ -1,7 +1,7 @@
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", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/edit", :type => :view do
5
5
  before(:each) do
6
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| -%>
@@ -1,7 +1,7 @@
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", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/index", :type => :view do
5
5
  before(:each) do
6
6
  assign(:<%= table_name %>, [
7
7
  <% [1,2].each_with_index do |id, model_index| -%>
@@ -1,7 +1,7 @@
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", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/new", :type => :view do
5
5
  before(:each) do
6
6
  assign(:<%= ns_file_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
7
7
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
@@ -1,7 +1,7 @@
1
- require "spec_helper"
1
+ require "rails_helper"
2
2
 
3
3
  <% module_namespacing do -%>
4
- describe <%= controller_class_name %>Controller, :type => :routing do
4
+ RSpec.describe <%= controller_class_name %>Controller, :type => :routing do
5
5
  describe "routing" do
6
6
 
7
7
  <% unless options[:singleton] -%>
@@ -1,7 +1,7 @@
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", :type => :view do
4
+ RSpec.describe "<%= ns_table_name %>/show", :type => :view do
5
5
  before(:each) do
6
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| -%>
@@ -15,7 +15,7 @@ describe "<%= ns_table_name %>/show", :type => :view do
15
15
  it "renders attributes in <p>" do
16
16
  render
17
17
  <% for attribute in output_attributes -%>
18
- expect(rendered).to match(/<%= eval(value_for(attribute)) %>/)
18
+ expect(rendered).to match(/<%= raw_value_for(attribute) %>/)
19
19
  <% end -%>
20
20
  end
21
21
  end
@@ -1,5 +1,5 @@
1
- require 'spec_helper'
1
+ require 'rails_helper'
2
2
 
3
- describe "<%= file_path %>/<%= @action %>", :type => :view do
3
+ RSpec.describe "<%= file_path %>/<%= @action %>", :type => :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,5 +1,8 @@
1
+ # Namespace for all core RSpec projects.
1
2
  module RSpec
3
+ # Namespace for rspec-rails code.
2
4
  module Rails
5
+ # Railtie to hook into Rails.
3
6
  class Railtie < ::Rails::Railtie
4
7
  # Rails-3.0.1 requires config.app_generators instead of 3.0.0's config.generators
5
8
  generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
@@ -7,6 +7,8 @@ module RSpec
7
7
  if ::Rails::VERSION::STRING >= '4.1.0'
8
8
  gem 'minitest'
9
9
  require 'minitest/assertions'
10
+ # Constant aliased to either Minitest or TestUnit, depending on what is
11
+ # loaded.
10
12
  Assertions = Minitest::Assertions
11
13
  else
12
14
  begin
@@ -16,12 +18,13 @@ module RSpec
16
18
  require 'rubysl-test-unit' if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
17
19
  require 'test/unit/assertions'
18
20
  end
21
+ # Constant aliased to either Minitest or TestUnit, depending on what is
22
+ # loaded.
19
23
  Assertions = Test::Unit::Assertions
20
24
  end
21
25
 
22
- # @api private
26
+ # @private
23
27
  class AssertionDelegator < Module
24
- # @api private
25
28
  def initialize(*assertion_modules)
26
29
  assertion_class = Class.new(SimpleDelegator) do
27
30
  include ::RSpec::Rails::Assertions
@@ -30,12 +33,10 @@ module RSpec
30
33
  end
31
34
 
32
35
  super() do
33
- # @api private
34
36
  define_method :build_assertion_instance do
35
37
  assertion_class.new(self)
36
38
  end
37
39
 
38
- # @api private
39
40
  def assertion_instance
40
41
  @assertion_instance ||= build_assertion_instance
41
42
  end
@@ -54,7 +55,7 @@ module RSpec
54
55
 
55
56
  # Adapts example groups for `Minitest::Test::LifecycleHooks`
56
57
  #
57
- # @api private
58
+ # @private
58
59
  module MinitestLifecycleAdapter
59
60
  extend ActiveSupport::Concern
60
61
 
@@ -82,26 +83,22 @@ module RSpec
82
83
  end
83
84
  end
84
85
 
85
- # @api private
86
+ # @private
86
87
  module MinitestCounters
87
- # @api private
88
88
  def assertions
89
89
  @assertions ||= 0
90
90
  end
91
91
 
92
- # @api private
93
92
  def assertions=(assertions)
94
93
  @assertions = assertions
95
94
  end
96
95
  end
97
96
 
98
- # @api private
97
+ # @private
99
98
  module SetupAndTeardownAdapter
100
99
  extend ActiveSupport::Concern
101
100
 
102
101
  module ClassMethods
103
- # @api private
104
- #
105
102
  # Wraps `setup` calls from within Rails' testing framework in `before`
106
103
  # hooks.
107
104
  def setup(*methods)
@@ -123,7 +120,6 @@ module RSpec
123
120
  end
124
121
  end
125
122
 
126
- # @api private
127
123
  def method_name
128
124
  @example
129
125
  end
@@ -133,9 +129,8 @@ module RSpec
133
129
  module MinitestAssertionAdapter
134
130
  extend ActiveSupport::Concern
135
131
 
132
+ # @private
136
133
  module ClassMethods
137
- # @api private
138
- #
139
134
  # Returns the names of assertion methods that we want to expose to
140
135
  # examples without exposing non-assertion methods in Test::Unit or
141
136
  # Minitest.
@@ -144,7 +139,6 @@ module RSpec
144
139
  [:build_message]
145
140
  end
146
141
 
147
- # @api private
148
142
  def define_assertion_delegators
149
143
  assertion_method_names.each do |m|
150
144
  define_method(m.to_sym) do |*args, &block|
@@ -154,13 +148,11 @@ module RSpec
154
148
  end
155
149
  end
156
150
 
157
- # @api private
158
151
  class AssertionDelegator
159
152
  include ::RSpec::Rails::Assertions
160
153
  include ::RSpec::Rails::MinitestCounters
161
154
  end
162
155
 
163
- # @api private
164
156
  def assertion_delegator
165
157
  @assertion_delegator ||= AssertionDelegator.new
166
158
  end
@@ -173,7 +165,7 @@ module RSpec
173
165
  # Backwards compatibility. It's unlikely that anyone is using this
174
166
  # constant, but we had forgotten to mark it as `@private` earlier
175
167
  #
176
- # @api private
168
+ # @private
177
169
  TestUnitAssertionAdapter = MinitestAssertionAdapter
178
170
  end
179
171
  end
@@ -1,5 +1,41 @@
1
1
  module RSpec
2
+
2
3
  module Rails
4
+ # Fake class to document RSpec Rails configuration options. In practice,
5
+ # these are dynamically added to the normal RSpec configuration object.
6
+ class Configuration
7
+ # @!method infer_spec_type_from_file_location!
8
+ # Automatically tag specs in conventional directories with matching `type`
9
+ # metadata so that they have relevant helpers available to them. See
10
+ # `RSpec::Rails::DIRECTORY_MAPPINGS` for details on which metadata is
11
+ # applied to each directory.
12
+
13
+ # @!method render_views=(val)
14
+ #
15
+ # When set to `true`, controller specs will render the relevant view as
16
+ # well. Defaults to `false`.
17
+
18
+ # @!method render_views(val)
19
+ # Enables view rendering for controllers specs.
20
+
21
+ # @!method render_views?
22
+ # Reader for currently value of `render_views` setting.
23
+ end
24
+
25
+ # Mappings used by `infer_spec_type_from_file_location!`.
26
+ #
27
+ # @api private
28
+ DIRECTORY_MAPPINGS = {
29
+ :controller => %w[spec controllers],
30
+ :helper => %w[spec helpers],
31
+ :mailer => %w[spec mailers],
32
+ :model => %w[spec models],
33
+ :request => %w[spec (requests|integration|api)],
34
+ :routing => %w[spec routing],
35
+ :view => %w[spec views],
36
+ :feature => %w[spec features]
37
+ }
38
+
3
39
  # @private
4
40
  def self.initialize_configuration(config)
5
41
  config.backtrace_exclusion_patterns << /vendor\//
@@ -13,7 +49,7 @@ module RSpec
13
49
  config.include RSpec::Rails::ViewExampleGroup, :type => :view
14
50
  config.include RSpec::Rails::FeatureExampleGroup, :type => :feature
15
51
 
16
- if defined?(RSpec::Rails::MailerExampleGroup)
52
+ if defined?(ActionMailer)
17
53
  config.include RSpec::Rails::MailerExampleGroup, :type => :mailer
18
54
  end
19
55
 
@@ -27,11 +63,10 @@ module RSpec
27
63
  config.add_setting :global_fixtures
28
64
  config.add_setting :fixture_path
29
65
 
30
- # view rendering settings
31
66
  # This allows us to expose `render_views` as a config option even though it
32
67
  # breaks the convention of other options by using `render_views` as a
33
- # command (i.e. render_views = true), where it would normally be used as a
34
- # getter. This makes it easier for rspec-rails users because we use
68
+ # command (i.e. `render_views = true`), where it would normally be used
69
+ # as a getter. This makes it easier for rspec-rails users because we use
35
70
  # `render_views` directly in example groups, so this aligns the two APIs,
36
71
  # but requires this workaround:
37
72
  config.add_setting :rendering_views, :default => false
@@ -49,16 +84,7 @@ module RSpec
49
84
  end
50
85
 
51
86
  def config.infer_spec_type_from_file_location!
52
- {
53
- :controller => %w[spec controllers],
54
- :helper => %w[spec helpers],
55
- :mailer => %w[spec mailers],
56
- :model => %w[spec models],
57
- :request => %w[spec (requests|integration|api)],
58
- :routing => %w[spec routing],
59
- :view => %w[spec views],
60
- :feature => %w[spec features]
61
- }.each do |type, dir_parts|
87
+ DIRECTORY_MAPPINGS.each do |type, dir_parts|
62
88
  escaped_path = Regexp.compile(dir_parts.join('[\\\/]') + '[\\\/]')
63
89
  define_derived_metadata(:file_path => escaped_path) do |metadata|
64
90
  metadata[:type] ||= type
@@ -1,4 +1,5 @@
1
1
  module RSpec::Rails
2
+ # Container module for controller spec functionality.
2
3
  module ControllerExampleGroup
3
4
  extend ActiveSupport::Concern
4
5
  include RSpec::Rails::RailsExampleGroup
@@ -9,6 +10,7 @@ module RSpec::Rails
9
10
  include RSpec::Rails::Matchers::RoutingMatchers
10
11
  include RSpec::Rails::AssertionDelegator.new(ActionDispatch::Assertions::RoutingAssertions)
11
12
 
13
+ # Class-level DSL for controller specs.
12
14
  module ClassMethods
13
15
  # @private
14
16
  def controller_class
@@ -20,7 +22,7 @@ module RSpec::Rails
20
22
  # `body` in that context. Also sets up implicit routes for this
21
23
  # controller, that are separate from those defined in "config/routes.rb".
22
24
  #
23
- # @note Due to Ruby 1.8 scoping rules in anoymous subclasses, constants
25
+ # @note Due to Ruby 1.8 scoping rules in anonymous subclasses, constants
24
26
  # defined in `ApplicationController` must be fully qualified (e.g.
25
27
  # `ApplicationController::AccessDenied`) in the block passed to the
26
28
  # `controller` method. Any instance methods, filters, etc, that are
@@ -28,7 +30,6 @@ module RSpec::Rails
28
30
  # within the block.
29
31
  #
30
32
  # @example
31
- #
32
33
  # describe ApplicationController do
33
34
  # controller do
34
35
  # def index
@@ -66,7 +67,7 @@ module RSpec::Rails
66
67
  end
67
68
  end
68
69
  end
69
- new_controller_class.class_eval(&body)
70
+ new_controller_class.class_exec(&body)
70
71
  (class << self; self; end).__send__(:define_method, :controller_class) { new_controller_class }
71
72
 
72
73
  before do
@@ -97,7 +98,6 @@ module RSpec::Rails
97
98
  # is most useful when testing Rails engines.
98
99
  #
99
100
  # @example
100
- #
101
101
  # describe MyEngine::PostsController do
102
102
  # routes { MyEngine::Engine.routes }
103
103
  #
@@ -112,12 +112,15 @@ module RSpec::Rails
112
112
 
113
113
  attr_reader :controller, :routes
114
114
 
115
- # @api private
115
+ # @private
116
+ #
117
+ # RSpec Rails uses this to make Rails routes easily available to specs.
116
118
  def routes=(routes)
117
119
  @routes = routes
118
120
  assertion_instance.instance_variable_set(:@routes, routes)
119
121
  end
120
122
 
123
+ # @private
121
124
  module BypassRescue
122
125
  def rescue_with_handler(exception)
123
126
  raise exception
@@ -130,7 +133,6 @@ module RSpec::Rails
130
133
  # conditions.
131
134
  #
132
135
  # @example
133
- #
134
136
  # describe ProfilesController do
135
137
  # it "raises a 403 when a non-admin user tries to view another user's profile" do
136
138
  # profile = create_profile
@@ -1,8 +1,10 @@
1
1
  module RSpec::Rails
2
+ # Container module for routing spec functionality.
2
3
  module FeatureExampleGroup
3
4
  extend ActiveSupport::Concern
4
5
  include RSpec::Rails::RailsExampleGroup
5
6
 
7
+ # Default host to be used in Rails route helpers if none is specified.
6
8
  DEFAULT_HOST = "www.example.com"
7
9
 
8
10
  included do
@@ -17,6 +19,9 @@ module RSpec::Rails
17
19
  end
18
20
  end
19
21
 
22
+ # Shim to check for presence of Capybara. Will delegate if present, raise
23
+ # if not. We assume here that in most cases `visit` will be the first
24
+ # Capybara method called in a spec.
20
25
  def visit(*)
21
26
  if defined?(super)
22
27
  super
@@ -1,14 +1,15 @@
1
1
  require 'rspec/rails/view_assigns'
2
2
 
3
3
  module RSpec::Rails
4
+ # Container module for helper specs.
4
5
  module HelperExampleGroup
5
6
  extend ActiveSupport::Concern
6
7
  include RSpec::Rails::RailsExampleGroup
7
8
  include ActionView::TestCase::Behavior
8
9
  include RSpec::Rails::ViewAssigns
9
10
 
11
+ # @private
10
12
  module ClassMethods
11
- # @api private
12
13
  def determine_default_helper_class(ignore)
13
14
  described_class
14
15
  end
@@ -1,5 +1,15 @@
1
+ module RSpec::Rails
2
+ # Container module for mailer spec functionality. It is only available if
3
+ # ActionMailer has been loaded before it.
4
+ module MailerExampleGroup
5
+ # This blank module is only necessary for YARD processing. It doesn't
6
+ # handle the conditional `defined?` check below very well.
7
+ end
8
+ end
9
+
1
10
  if defined?(ActionMailer)
2
11
  module RSpec::Rails
12
+ # Container module for mailer spec functionality.
3
13
  module MailerExampleGroup
4
14
  extend ActiveSupport::Concern
5
15
  include RSpec::Rails::RailsExampleGroup
@@ -11,7 +21,9 @@ if defined?(ActionMailer)
11
21
  options.each { |key, value| default_url_options[key] = value } if options
12
22
  end
13
23
 
24
+ # Class-level DSL for mailer specs.
14
25
  module ClassMethods
26
+ # Alias for `described_class`.
15
27
  def mailer_class
16
28
  described_class
17
29
  end
@@ -1,4 +1,6 @@
1
1
  module RSpec::Rails
2
+ # Container class for model spec functionality. Does not provide anything
3
+ # special over the common RailsExampleGroup currently.
2
4
  module ModelExampleGroup
3
5
  extend ActiveSupport::Concern
4
6
  include RSpec::Rails::RailsExampleGroup
@@ -4,6 +4,7 @@ require 'rspec/rails/matchers'
4
4
 
5
5
  module RSpec
6
6
  module Rails
7
+ # Common rails example functionality.
7
8
  module RailsExampleGroup
8
9
  extend ActiveSupport::Concern
9
10
  include RSpec::Rails::SetupAndTeardownAdapter
@@ -1,4 +1,5 @@
1
1
  module RSpec::Rails
2
+ # Container class for request spec functionality.
2
3
  module RequestExampleGroup
3
4
  extend ActiveSupport::Concern
4
5
  include RSpec::Rails::RailsExampleGroup
@@ -8,6 +9,7 @@ module RSpec::Rails
8
9
  include RSpec::Rails::Matchers::RenderTemplate
9
10
  include ActionController::TemplateAssertions
10
11
 
12
+ # Delegates to `Rails.application`.
11
13
  def app
12
14
  ::Rails.application
13
15
  end