rspec-rails 2.3.1 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/History.md +21 -1
  2. data/README.md +3 -3
  3. data/Rakefile +2 -2
  4. data/{Upgrade.markdown → Upgrade.md} +0 -0
  5. data/features/.nav +12 -7
  6. data/features/Autotest.md +10 -0
  7. data/features/Generators.md +8 -0
  8. data/features/GettingStarted.md +40 -0
  9. data/features/{README.markdown → README.md} +1 -26
  10. data/features/Upgrade.md +56 -0
  11. data/features/controller_specs/README.md +27 -16
  12. data/features/controller_specs/isolation_from_views.feature +6 -5
  13. data/features/controller_specs/render_views.feature +30 -1
  14. data/features/helper_specs/helper_spec.feature +36 -3
  15. data/features/mocks/mock_model.feature +3 -3
  16. data/features/model_specs/README.md +19 -0
  17. data/features/routing_specs/README.md +16 -0
  18. data/features/{matchers → routing_specs}/be_routable_matcher.feature +18 -18
  19. data/features/routing_specs/{access_to_named_routes.feature → named_routes.feature} +6 -3
  20. data/features/routing_specs/route_to_matcher.feature +38 -0
  21. data/lib/generators/rspec/scaffold/scaffold_generator.rb +6 -6
  22. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +22 -24
  23. data/lib/rspec/rails.rb +0 -1
  24. data/lib/rspec/rails/example.rb +29 -1
  25. data/lib/rspec/rails/example/controller_example_group.rb +1 -5
  26. data/lib/rspec/rails/example/helper_example_group.rb +5 -6
  27. data/lib/rspec/rails/example/mailer_example_group.rb +1 -5
  28. data/lib/rspec/rails/example/model_example_group.rb +0 -3
  29. data/lib/rspec/rails/example/request_example_group.rb +1 -5
  30. data/lib/rspec/rails/example/routing_example_group.rb +1 -5
  31. data/lib/rspec/rails/example/view_example_group.rb +1 -4
  32. data/lib/rspec/rails/matchers/redirect_to.rb +6 -2
  33. data/lib/rspec/rails/matchers/render_template.rb +5 -1
  34. data/lib/rspec/rails/matchers/routing_matchers.rb +1 -1
  35. data/lib/rspec/rails/module_inclusion.rb +17 -0
  36. data/lib/rspec/rails/version.rb +1 -1
  37. data/lib/rspec/rails/view_rendering.rb +23 -7
  38. data/spec/rspec/rails/configuration_spec.rb +26 -0
  39. data/spec/rspec/rails/example/helper_example_group_spec.rb +18 -0
  40. data/spec/rspec/rails/example/request_example_group_spec.rb +2 -0
  41. data/spec/rspec/rails/matchers/be_routable_spec.rb +41 -0
  42. data/spec/rspec/rails/matchers/redirect_to_spec.rb +67 -8
  43. data/spec/rspec/rails/matchers/render_template_spec.rb +67 -10
  44. data/spec/rspec/rails/matchers/route_to_spec.rb +64 -33
  45. data/spec/rspec/rails/view_rendering_spec.rb +105 -0
  46. metadata +38 -20
  47. data/spec/rspec/rails/example/view_rendering_spec.rb +0 -110
@@ -13,12 +13,10 @@ module RSpec::Rails
13
13
  # => delegates to assert_redirected_to(destination)
14
14
  module RequestExampleGroup
15
15
  extend ActiveSupport::Concern
16
- extend RSpec::Rails::ModuleInclusion
17
-
18
- include RSpec::Rails::RailsExampleGroup
19
16
 
20
17
  include ActionDispatch::Integration::Runner
21
18
  include ActionDispatch::Assertions
19
+ include RSpec::Rails::RailsExampleGroup
22
20
  include RSpec::Rails::BrowserSimulators
23
21
 
24
22
  module InstanceMethods
@@ -62,7 +60,5 @@ module RSpec::Rails
62
60
  end
63
61
  end
64
62
  end
65
-
66
- RSpec.configure &include_self_when_dir_matches('spec','requests')
67
63
  end
68
64
  end
@@ -3,11 +3,9 @@ require "action_dispatch/testing/assertions/routing"
3
3
  module RSpec::Rails
4
4
  module RoutingExampleGroup
5
5
  extend ActiveSupport::Concern
6
- extend RSpec::Rails::ModuleInclusion
7
-
8
- include RSpec::Rails::RailsExampleGroup
9
6
 
10
7
  include ActionDispatch::Assertions::RoutingAssertions
8
+ include RSpec::Rails::RailsExampleGroup
11
9
  include RSpec::Rails::Matchers::RoutingMatchers
12
10
 
13
11
  module InstanceMethods
@@ -27,7 +25,5 @@ module RSpec::Rails
27
25
  @routes = ::Rails.application.routes
28
26
  end
29
27
  end
30
-
31
- RSpec.configure &include_self_when_dir_matches('spec','routing')
32
28
  end
33
29
  end
@@ -19,10 +19,9 @@ module RSpec::Rails
19
19
  # end
20
20
  module ViewExampleGroup
21
21
  extend ActiveSupport::Concern
22
- extend RSpec::Rails::ModuleInclusion
23
22
 
24
- include RSpec::Rails::RailsExampleGroup
25
23
  include ActionView::TestCase::Behavior
24
+ include RSpec::Rails::RailsExampleGroup
26
25
  include RSpec::Rails::ViewAssigns
27
26
  include RSpec::Rails::Matchers::RenderTemplate
28
27
  include RSpec::Rails::BrowserSimulators
@@ -154,8 +153,6 @@ module RSpec::Rails
154
153
  controller.request.path_parameters["action"] = _inferred_action unless _inferred_action =~ /^_/
155
154
  end
156
155
  end
157
-
158
- RSpec.configure &include_self_when_dir_matches('spec','views')
159
156
  end
160
157
  end
161
158
 
@@ -3,13 +3,17 @@ module RSpec::Rails::Matchers
3
3
  extend RSpec::Matchers::DSL
4
4
 
5
5
  matcher :redirect_to do |destination|
6
- match_unless_raises Test::Unit::AssertionFailedError do |_|
6
+ match_unless_raises ActiveSupport::TestCase::Assertion do |_|
7
7
  assert_redirected_to destination
8
8
  end
9
9
 
10
- failure_message_for_should do
10
+ failure_message_for_should do |_|
11
11
  rescued_exception.message
12
12
  end
13
+
14
+ failure_message_for_should_not do |_|
15
+ "expected not to redirect to #{destination.inspect}, but did"
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -3,7 +3,7 @@ module RSpec::Rails::Matchers
3
3
  extend RSpec::Matchers::DSL
4
4
 
5
5
  matcher :render_template do |options, message|
6
- match_unless_raises Test::Unit::AssertionFailedError do |_|
6
+ match_unless_raises ActiveSupport::TestCase::Assertion do |_|
7
7
  options = options.to_s if Symbol === options
8
8
  assert_template options, message
9
9
  end
@@ -11,6 +11,10 @@ module RSpec::Rails::Matchers
11
11
  failure_message_for_should do
12
12
  rescued_exception.message
13
13
  end
14
+
15
+ failure_message_for_should_not do |_|
16
+ "expected not to render #{options.inspect}, but did"
17
+ end
14
18
  end
15
19
  end
16
20
  end
@@ -3,7 +3,7 @@ module RSpec::Rails::Matchers
3
3
  extend RSpec::Matchers::DSL
4
4
 
5
5
  matcher :route_to do |route_options|
6
- match_unless_raises Test::Unit::AssertionFailedError do |path|
6
+ match_unless_raises ActiveSupport::TestCase::Assertion do |path|
7
7
  assertion_path = { :method => path.keys.first, :path => path.values.first }
8
8
  assert_recognizes(route_options, assertion_path)
9
9
  end
@@ -1,7 +1,24 @@
1
1
  module RSpec::Rails
2
2
  module ModuleInclusion
3
+ # Deprecated as of rspec-rails-2.4
4
+ # Will be removed from rspec-rails-3.0
5
+ #
6
+ # This was never intended to be a public API and is no longer needed
7
+ # internally. As it happens, there are a few blog posts citing its use, so
8
+ # I'm leaving it here, but deprecated.
3
9
  def include_self_when_dir_matches(*path_parts)
10
+ instead = <<-INSTEAD
11
+
12
+
13
+ RSpec.configure do |c|
14
+ c.include self, :example_group => {
15
+ :file_path => /#{path_parts.join('\/')}/
16
+ }
17
+ end
18
+
19
+ INSTEAD
4
20
  lambda do |c|
21
+ RSpec.deprecate('include_self_when_dir_matches', instead, 'rails-3.0')
5
22
  c.include self, :example_group => {
6
23
  :file_path => Regexp.compile(path_parts.join('[\\\/]'))
7
24
  }
@@ -1,7 +1,7 @@
1
1
  module RSpec # :nodoc:
2
2
  module Rails # :nodoc:
3
3
  module Version # :nodoc:
4
- STRING = '2.3.1'
4
+ STRING = '2.4.0'
5
5
  end
6
6
  end
7
7
  end
@@ -1,18 +1,34 @@
1
1
  require 'action_view/testing/resolvers'
2
2
 
3
+ RSpec.configure do |config|
4
+ config.add_setting :render_views, :default => false
5
+
6
+ # TODO - rspec-core needs a way to define a setting that works like this in
7
+ # one go
8
+ def config.render_views
9
+ settings[:render_views] = true
10
+ end
11
+
12
+ def config.render_views?
13
+ settings[:render_views]
14
+ end
15
+ end
16
+
3
17
  module RSpec
4
18
  module Rails
5
19
  module ViewRendering
6
20
  extend ActiveSupport::Concern
7
21
 
22
+ attr_accessor :controller
23
+
8
24
  module ClassMethods
9
25
  def metadata_for_rspec_rails
10
26
  metadata[:rspec_rails] ||= {}
11
27
  end
12
28
 
13
29
  # See RSpec::Rails::ControllerExampleGroup
14
- def render_views
15
- metadata_for_rspec_rails[:render_views] = true
30
+ def render_views(true_or_false=true)
31
+ metadata_for_rspec_rails[:render_views] = true_or_false
16
32
  end
17
33
 
18
34
  def integrate_views
@@ -21,13 +37,13 @@ module RSpec
21
37
  end
22
38
 
23
39
  def render_views?
24
- !!metadata_for_rspec_rails[:render_views]
40
+ metadata_for_rspec_rails[:render_views] || RSpec.configuration.render_views?
25
41
  end
26
42
  end
27
43
 
28
44
  module InstanceMethods
29
45
  def render_views?
30
- self.class.render_views? || !@controller.class.respond_to?(:view_paths)
46
+ self.class.render_views? || !controller.class.respond_to?(:view_paths)
31
47
  end
32
48
  end
33
49
 
@@ -58,14 +74,14 @@ module RSpec
58
74
  included do
59
75
  before do
60
76
  unless render_views?
61
- @_path_set_delegator_resolver = PathSetDelegatorResolver.new(@controller.class.view_paths)
62
- @controller.class.view_paths = ::ActionView::PathSet.new.push(@_path_set_delegator_resolver)
77
+ @_path_set_delegator_resolver = PathSetDelegatorResolver.new(controller.class.view_paths)
78
+ controller.class.view_paths = ::ActionView::PathSet.new.push(@_path_set_delegator_resolver)
63
79
  end
64
80
  end
65
81
 
66
82
  after do
67
83
  unless render_views?
68
- @controller.class.view_paths = @_path_set_delegator_resolver.path_set
84
+ controller.class.view_paths = @_path_set_delegator_resolver.path_set
69
85
  end
70
86
  end
71
87
  end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ describe "configuration" do
4
+ before do
5
+ @orig_render_views = RSpec.configuration.render_views?
6
+ end
7
+
8
+ after do
9
+ RSpec.configuration.render_views = @orig_render_views
10
+ end
11
+
12
+ describe "#render_views?" do
13
+ it "is false by default" do
14
+ RSpec.configuration.render_views?.should be_false
15
+ end
16
+ end
17
+
18
+ describe "#render_views" do
19
+ it "sets render_views? to return true" do
20
+ RSpec.configuration.render_views = false
21
+ RSpec.configuration.render_views
22
+
23
+ RSpec.configuration.render_views?.should be_true
24
+ end
25
+ end
26
+ end
@@ -30,6 +30,24 @@ module RSpec::Rails
30
30
  helper_spec.stub(:_view) { av_tc_b_view }
31
31
  helper_spec.helper.should eq(av_tc_b_view)
32
32
  end
33
+
34
+ before do
35
+ Object.const_set(:ApplicationHelper, Module.new)
36
+ end
37
+
38
+ after do
39
+ Object.__send__(:remove_const, :ApplicationHelper)
40
+ end
41
+
42
+ it "includes ApplicationHelper" do
43
+ group = RSpec::Core::ExampleGroup.describe do
44
+ include HelperExampleGroup
45
+ def _view
46
+ ActionView::Base.new
47
+ end
48
+ end
49
+ group.new.helper.should be_kind_of(ApplicationHelper)
50
+ end
33
51
  end
34
52
  end
35
53
 
@@ -3,7 +3,9 @@ require "spec_helper"
3
3
  module RSpec::Rails
4
4
  describe RequestExampleGroup do
5
5
  it { should be_included_in_files_in('./spec/requests/') }
6
+ it { should be_included_in_files_in('./spec/integration/') }
6
7
  it { should be_included_in_files_in('.\\spec\\requests\\') }
8
+ it { should be_included_in_files_in('.\\spec\\integration\\') }
7
9
 
8
10
  it "adds :type => :request to the metadata" do
9
11
  group = RSpec::Core::ExampleGroup.describe do
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe "be_routable" do
4
+ include RSpec::Rails::Matchers::RoutingMatchers
5
+ attr_reader :routes
6
+
7
+ before { @routes = double("routes") }
8
+
9
+ context "with should" do
10
+ it "passes if routes recognize the path" do
11
+ routes.stub(:recognize_path) { {} }
12
+ expect do
13
+ {:get => "/a/path"}.should be_routable
14
+ end.to_not raise_error
15
+ end
16
+
17
+ it "fails if routes do not recognize the path" do
18
+ routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
19
+ expect do
20
+ {:get => "/a/path"}.should be_routable
21
+ end.to raise_error(/expected \{:get=>"\/a\/path"\} to be routable/)
22
+ end
23
+ end
24
+
25
+ context "with should_not" do
26
+
27
+ it "passes if routes do not recognize the path" do
28
+ routes.stub(:recognize_path) { raise ActionController::RoutingError.new('ignore') }
29
+ expect do
30
+ {:get => "/a/path"}.should_not be_routable
31
+ end.to_not raise_error
32
+ end
33
+
34
+ it "fails if routes recognize the path" do
35
+ routes.stub(:recognize_path) { {:controller => "foo"} }
36
+ expect do
37
+ {:get => "/a/path"}.should_not be_routable
38
+ end.to raise_error(/expected \{:get=>"\/a\/path"\} not to be routable, but it routes to \{:controller=>"foo"\}/)
39
+ end
40
+ end
41
+ end
@@ -1,20 +1,79 @@
1
1
  require "spec_helper"
2
- require "action_controller/test_case"
2
+ require "active_support/test_case"
3
3
 
4
4
  describe "redirect_to" do
5
5
  include RSpec::Rails::Matchers::RedirectTo
6
6
 
7
+ let(:response) { ActionController::TestResponse.new }
8
+
7
9
  it "delegates to assert_redirected_to" do
8
10
  self.should_receive(:assert_redirected_to).with("destination")
9
11
  "response".should redirect_to("destination")
10
12
  end
11
13
 
12
- it "uses failure message from assert_redirected_to" do
13
- self.stub!(:assert_redirected_to).and_raise(
14
- Test::Unit::AssertionFailedError.new("this message"))
15
- response = ActionController::TestResponse.new
16
- expect do
17
- response.should redirect_to("destination")
18
- end.to raise_error("this message")
14
+ context "with should" do
15
+ context "when assert_redirected_to passes" do
16
+ it "passes" do
17
+ self.stub!(:assert_redirected_to)
18
+ expect do
19
+ response.should redirect_to("destination")
20
+ end.to_not raise_exception
21
+ end
22
+ end
23
+
24
+ context "when assert_redirected_to fails" do
25
+ it "uses failure message from assert_redirected_to" do
26
+ self.stub!(:assert_redirected_to) do
27
+ raise ActiveSupport::TestCase::Assertion.new("this message")
28
+ end
29
+ expect do
30
+ response.should redirect_to("destination")
31
+ end.to raise_error("this message")
32
+ end
33
+ end
34
+
35
+ context "when fails due to some other exception" do
36
+ it "raises that exception" do
37
+ self.stub!(:assert_redirected_to) do
38
+ raise "oops"
39
+ end
40
+ expect do
41
+ response.should redirect_to("destination")
42
+ end.to raise_exception("oops")
43
+ end
44
+ end
45
+ end
46
+
47
+ context "with should_not" do
48
+ context "when assert_redirected_to fails" do
49
+ it "passes" do
50
+ self.stub!(:assert_redirected_to) do
51
+ raise ActiveSupport::TestCase::Assertion.new("this message")
52
+ end
53
+ expect do
54
+ response.should_not redirect_to("destination")
55
+ end.to_not raise_exception
56
+ end
57
+ end
58
+
59
+ context "when assert_redirected_to passes" do
60
+ it "fails with custom failure message" do
61
+ self.stub!(:assert_redirected_to)
62
+ expect do
63
+ response.should_not redirect_to("destination")
64
+ end.to raise_error(/expected not to redirect to \"destination\", but did/)
65
+ end
66
+ end
67
+
68
+ context "when fails due to some other exception" do
69
+ it "raises that exception" do
70
+ self.stub!(:assert_redirected_to) do
71
+ raise "oops"
72
+ end
73
+ expect do
74
+ response.should_not redirect_to("destination")
75
+ end.to raise_exception("oops")
76
+ end
77
+ end
19
78
  end
20
79
  end
@@ -2,15 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe "render_template" do
4
4
  include RSpec::Rails::Matchers::RenderTemplate
5
-
6
- it "uses failure message from render_template" do
7
- self.stub!(:assert_template).and_raise(
8
- Test::Unit::AssertionFailedError.new("this message"))
9
- response = ActionController::TestResponse.new
10
- expect do
11
- response.should render_template("destination")
12
- end.to raise_error("this message")
13
- end
5
+ let(:response) { ActionController::TestResponse.new }
14
6
 
15
7
  context "given a hash" do
16
8
  it "delegates to assert_template" do
@@ -32,5 +24,70 @@ describe "render_template" do
32
24
  "response".should render_template(:template_name, "this message")
33
25
  end
34
26
  end
35
- end
36
27
 
28
+ context "with should" do
29
+ context "when assert_template passes" do
30
+ it "passes" do
31
+ self.stub!(:assert_template)
32
+ expect do
33
+ response.should render_template("template_name")
34
+ end.to_not raise_exception
35
+ end
36
+ end
37
+
38
+ context "when assert_template fails" do
39
+ it "uses failure message from assert_template" do
40
+ self.stub!(:assert_template) do
41
+ raise ActiveSupport::TestCase::Assertion.new("this message")
42
+ end
43
+ expect do
44
+ response.should render_template("template_name")
45
+ end.to raise_error("this message")
46
+ end
47
+ end
48
+
49
+ context "when fails due to some other exception" do
50
+ it "raises that exception" do
51
+ self.stub!(:assert_template) do
52
+ raise "oops"
53
+ end
54
+ expect do
55
+ response.should render_template("template_name")
56
+ end.to raise_exception("oops")
57
+ end
58
+ end
59
+ end
60
+
61
+ context "with should_not" do
62
+ context "when assert_template fails" do
63
+ it "passes" do
64
+ self.stub!(:assert_template) do
65
+ raise ActiveSupport::TestCase::Assertion.new("this message")
66
+ end
67
+ expect do
68
+ response.should_not render_template("template_name")
69
+ end.to_not raise_exception
70
+ end
71
+ end
72
+
73
+ context "when assert_template passes" do
74
+ it "fails with custom failure message" do
75
+ self.stub!(:assert_template)
76
+ expect do
77
+ response.should_not render_template("template_name")
78
+ end.to raise_error(/expected not to render \"template_name\", but did/)
79
+ end
80
+ end
81
+
82
+ context "when fails due to some other exception" do
83
+ it "raises that exception" do
84
+ self.stub!(:assert_template) do
85
+ raise "oops"
86
+ end
87
+ expect do
88
+ response.should_not render_template("template_name")
89
+ end.to raise_exception("oops")
90
+ end
91
+ end
92
+ end
93
+ end