rspec-rails 2.0.0.beta.8 → 2.0.0.beta.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 (61) hide show
  1. data/README.markdown +82 -27
  2. data/Rakefile +7 -7
  3. data/Upgrade.markdown +12 -0
  4. data/VERSION +1 -1
  5. data/autotest/discover.rb +1 -0
  6. data/cucumber.yml +3 -0
  7. data/example_app_template.rb +2 -2
  8. data/features/controller_specs/do_not_render_views.feature +61 -0
  9. data/features/controller_specs/readers.feature +18 -0
  10. data/features/controller_specs/render_views.feature +53 -0
  11. data/features/model_specs/errors_on.feature +32 -0
  12. data/features/model_specs/transactional_examples.feature +2 -2
  13. data/features/view_specs/view_spec.feature +102 -1
  14. data/lib/autotest/rails_rspec2.rb +1 -1
  15. data/lib/generators/rspec/helper/helper_generator.rb +1 -0
  16. data/lib/generators/rspec/helper/templates/helper_spec.rb +3 -3
  17. data/lib/generators/rspec/install/install_generator.rb +7 -7
  18. data/lib/generators/rspec/scaffold/scaffold_generator.rb +10 -1
  19. data/lib/generators/rspec/scaffold/templates/edit_spec.rb +1 -1
  20. data/lib/generators/rspec/scaffold/templates/index_spec.rb +1 -1
  21. data/lib/generators/rspec/scaffold/templates/new_spec.rb +1 -1
  22. data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
  23. data/lib/rspec-rails.rb +12 -0
  24. data/lib/rspec/rails.rb +8 -0
  25. data/lib/rspec/rails/adapters.rb +12 -8
  26. data/lib/rspec/rails/example.rb +1 -0
  27. data/lib/rspec/rails/example/controller_example_group.rb +13 -12
  28. data/lib/rspec/rails/example/helper_example_group.rb +43 -0
  29. data/lib/rspec/rails/example/mailer_example_group.rb +10 -5
  30. data/lib/rspec/rails/example/request_example_group.rb +7 -7
  31. data/lib/rspec/rails/example/view_example_group.rb +44 -62
  32. data/lib/rspec/rails/extensions.rb +1 -0
  33. data/lib/rspec/rails/extensions/active_record/base.rb +46 -0
  34. data/lib/rspec/rails/matchers.rb +28 -3
  35. data/lib/rspec/rails/mocks.rb +3 -3
  36. data/lib/rspec/rails/monkey.rb +1 -0
  37. data/lib/rspec/rails/monkey/action_controller/test_case.rb +153 -145
  38. data/lib/rspec/rails/monkey/action_view/test_case.rb +201 -0
  39. data/lib/rspec/rails/null_resolver.rb +10 -0
  40. data/lib/{generators/rspec/install/templates/lib → rspec/rails}/tasks/rspec.rake +4 -5
  41. data/lib/rspec/rails/transactional_database_support.rb +4 -6
  42. data/lib/rspec/rails/version.rb +1 -1
  43. data/lib/rspec/rails/view_assigns.rb +28 -0
  44. data/lib/rspec/rails/view_rendering.rb +33 -0
  45. data/rspec-rails.gemspec +41 -11
  46. data/spec/rspec/rails/example/controller_example_group_spec.rb +11 -0
  47. data/spec/rspec/rails/example/helper_example_group_spec.rb +20 -0
  48. data/spec/rspec/rails/example/mailer_example_group_spec.rb +11 -0
  49. data/spec/rspec/rails/example/request_example_group_spec.rb +11 -0
  50. data/spec/rspec/rails/example/view_example_group_spec.rb +75 -0
  51. data/spec/rspec/rails/example/view_rendering_spec.rb +68 -0
  52. data/spec/rspec/rails/matchers/errors_on_spec.rb +38 -0
  53. data/spec/rspec/rails/mocks/ar_classes.rb +1 -1
  54. data/spec/rspec/rails/mocks/mock_model_spec.rb +88 -65
  55. data/spec/rspec/rails/mocks/stub_model_spec.rb +1 -1
  56. data/spec/rspec/rails/transactional_database_support_spec.rb +2 -2
  57. data/spec/spec_helper.rb +18 -2
  58. data/spec/support/helpers.rb +20 -0
  59. data/specs.watchr +10 -10
  60. data/templates/generate_stuff.rb +3 -1
  61. metadata +40 -9
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe ControllerExampleGroupBehaviour do
4
+ it "is included in specs in ./spec/controllers" do
5
+ stub_metadata(
6
+ :example_group => {:file_path => "./spec/controllers/whatever_spec.rb:15"}
7
+ )
8
+ group = RSpec::Core::ExampleGroup.describe
9
+ group.included_modules.should include(ControllerExampleGroupBehaviour)
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ describe HelperExampleGroupBehaviour do
4
+ it "is included in specs in ./spec/views" do
5
+ stub_metadata(
6
+ :example_group => {:file_path => "./spec/helpers/whatever_spec.rb:15"}
7
+ )
8
+ group = RSpec::Core::ExampleGroup.describe
9
+ group.included_modules.should include(HelperExampleGroupBehaviour)
10
+ end
11
+
12
+ module ::FoosHelper; end
13
+
14
+ it "provides a controller_path based on the helper module's name" do
15
+ helper_spec = Object.new
16
+ helper_spec.extend HelperExampleGroupBehaviour::InstanceMethods
17
+ helper_spec.stub_chain(:running_example, :example_group, :describes).and_return(FoosHelper)
18
+ helper_spec.__send__(:_controller_path).should == "foos"
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe MailerExampleGroupBehaviour do
4
+ it "is included in specs in ./spec/mailers" do
5
+ stub_metadata(
6
+ :example_group => {:file_path => "./spec/mailers/whatever_spec.rb:15"}
7
+ )
8
+ group = RSpec::Core::ExampleGroup.describe
9
+ group.included_modules.should include(MailerExampleGroupBehaviour)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe RequestExampleGroupBehaviour do
4
+ it "is included in specs in ./spec/requests" do
5
+ stub_metadata(
6
+ :example_group => {:file_path => "./spec/requests/whatever_spec.rb:15"}
7
+ )
8
+ group = RSpec::Core::ExampleGroup.describe
9
+ group.included_modules.should include(RequestExampleGroupBehaviour)
10
+ end
11
+ end
@@ -0,0 +1,75 @@
1
+ require "spec_helper"
2
+
3
+ describe ViewExampleGroupBehaviour do
4
+ it "is included in specs in ./spec/views" do
5
+ stub_metadata(
6
+ :example_group => {:file_path => "./spec/views/whatever_spec.rb:15"}
7
+ )
8
+ group = RSpec::Core::ExampleGroup.describe
9
+ group.included_modules.should include(ViewExampleGroupBehaviour)
10
+ end
11
+
12
+ describe "#render" do
13
+ let(:view_spec) do
14
+ Class.new do
15
+ module Local
16
+ def received
17
+ @received ||= []
18
+ end
19
+ def render(options={}, local_assigns={}, &block)
20
+ received << [options, local_assigns, block]
21
+ end
22
+ end
23
+ include Local
24
+ include ViewExampleGroupBehaviour::InstanceMethods
25
+ end.new
26
+ end
27
+
28
+ context "given no input" do
29
+ it "sends render(:file => (described file)) to the view" do
30
+ view_spec.stub(:_default_file_to_render) { "widgets/new.html.erb" }
31
+ view_spec.render
32
+ view_spec.received.first.should == [{:template => "widgets/new.html.erb"},{}, nil]
33
+ end
34
+ end
35
+
36
+ context "given a string" do
37
+ it "sends string as the first arg to render" do
38
+ view_spec.render('arbitrary/path')
39
+ view_spec.received.first.should == ["arbitrary/path", {}, nil]
40
+ end
41
+ end
42
+
43
+ context "given a hash" do
44
+ it "sends the hash as the first arg to render" do
45
+ view_spec.render(:foo => 'bar')
46
+ view_spec.received.first.should == [{:foo => "bar"}, {}, nil]
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "#_controller_path" do
52
+ let(:view_spec) do
53
+ Class.new do
54
+ include ViewExampleGroupBehaviour::InstanceMethods
55
+ end.new
56
+ end
57
+ context "with a common _default_file_to_render" do
58
+ it "it returns the directory" do
59
+ view_spec.stub(:_default_file_to_render).
60
+ and_return("things/new.html.erb")
61
+ view_spec.__send__(:_controller_path).
62
+ should == "things"
63
+ end
64
+ end
65
+
66
+ context "with a nested _default_file_to_render" do
67
+ it "it returns the directory path" do
68
+ view_spec.stub(:_default_file_to_render).
69
+ and_return("admin/things/new.html.erb")
70
+ view_spec.__send__(:_controller_path).
71
+ should == "admin/things"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe RSpec::Rails::ViewRendering do
4
+ it "doesn't render views by default" do
5
+ rendering_views = nil
6
+ group = RSpec::Core::ExampleGroup.describe do
7
+ include ControllerExampleGroupBehaviour
8
+ rendering_views = render_views?
9
+ it("does something") {}
10
+ end
11
+ group.run(double.as_null_object)
12
+ rendering_views.should be_false
13
+ end
14
+
15
+ it "doesn't render views by default in a nested group" do
16
+ rendering_views = nil
17
+ group = RSpec::Core::ExampleGroup.describe do
18
+ include ControllerExampleGroupBehaviour
19
+ describe "nested" do
20
+ rendering_views = render_views?
21
+ it("does something") {}
22
+ end
23
+ end
24
+ group.run(double.as_null_object)
25
+ rendering_views.should be_false
26
+ end
27
+
28
+ it "renders views if told to" do
29
+ rendering_views = false
30
+ group = RSpec::Core::ExampleGroup.describe do
31
+ include ControllerExampleGroupBehaviour
32
+ render_views
33
+ rendering_views = render_views?
34
+ it("does something") {}
35
+ end
36
+ group.run(double.as_null_object)
37
+ rendering_views.should be_true
38
+ end
39
+
40
+ it "renders views if told to in a nested group" do
41
+ rendering_views = nil
42
+ group = RSpec::Core::ExampleGroup.describe do
43
+ include ControllerExampleGroupBehaviour
44
+ describe "nested" do
45
+ render_views
46
+ rendering_views = render_views?
47
+ it("does something") {}
48
+ end
49
+ end
50
+ group.run(double.as_null_object)
51
+ rendering_views.should be_true
52
+ end
53
+
54
+ it "renders views in a nested group if told to in an outer group" do
55
+ rendering_views = nil
56
+ group = RSpec::Core::ExampleGroup.describe do
57
+ include ControllerExampleGroupBehaviour
58
+ render_views
59
+ describe "nested" do
60
+ rendering_views = render_views?
61
+ it("does something") {}
62
+ end
63
+ end
64
+ group.run(double.as_null_object)
65
+ rendering_views.should be_true
66
+ end
67
+ end
68
+
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe "error_on" do
4
+ it "should provide a description including the name of what the error is on" do
5
+ have(1).error_on(:whatever).description.should == "have 1 error on :whatever"
6
+ end
7
+
8
+ it "should provide a failure message including the number actually given" do
9
+ lambda {
10
+ [].should have(1).error_on(:whatever)
11
+ }.should raise_error("expected 1 error on :whatever, got 0")
12
+ end
13
+ end
14
+
15
+ describe "errors_on" do
16
+ it "should provide a description including the name of what the error is on" do
17
+ have(2).errors_on(:whatever).description.should == "have 2 errors on :whatever"
18
+ end
19
+
20
+ it "should provide a failure message including the number actually given" do
21
+ lambda {
22
+ [1].should have(3).errors_on(:whatever)
23
+ }.should raise_error("expected 3 errors on :whatever, got 1")
24
+ end
25
+ end
26
+
27
+ describe "have something other than error_on or errors_on" do
28
+ it "has a standard rspec failure message" do
29
+ lambda {
30
+ [1,2,3].should have(2).elements
31
+ }.should raise_error("expected 2 elements, got 3")
32
+ end
33
+
34
+ it "has a standard rspec description" do
35
+ have(2).elements.description.should == "have 2 elements"
36
+ end
37
+ end
38
+
@@ -6,7 +6,7 @@ module NoConnections
6
6
  end
7
7
 
8
8
  def connection
9
- Rspec::Mocks::Mock.new.as_null_object
9
+ RSpec::Mocks::Mock.new.as_null_object
10
10
  end
11
11
  end
12
12
  end
@@ -1,116 +1,142 @@
1
1
  require 'spec_helper'
2
2
  require File.dirname(__FILE__) + '/ar_classes'
3
3
 
4
- describe "mock_model" do
5
- describe "responding to interrogation" do
4
+ describe "mock_model(RealModel)" do
5
+ describe "with #id stubbed" do
6
6
  before(:each) do
7
- @model = mock_model(SubMockableModel)
8
- end
9
- it "should say it is_a? if it is" do
10
- @model.is_a?(SubMockableModel).should be(true)
11
- end
12
- it "should say it is_a? if it's ancestor is" do
13
- @model.is_a?(MockableModel).should be(true)
14
- end
15
- it "should say it is kind_of? if it is" do
16
- @model.kind_of?(SubMockableModel).should be(true)
17
- end
18
- it "should say it is kind_of? if it's ancestor is" do
19
- @model.kind_of?(MockableModel).should be(true)
20
- end
21
- it "should say it is instance_of? if it is" do
22
- @model.instance_of?(SubMockableModel).should be(true)
23
- end
24
- it "should not say it instance_of? if it isn't, even if it's ancestor is" do
25
- @model.instance_of?(MockableModel).should be(false)
7
+ @model = mock_model(MockableModel, :id => 1)
26
8
  end
27
- it "should say it is not destroyed" do
28
- @model.destroyed?(SubMockableModel).should be(false)
9
+ it "is named using the stubbed id value" do
10
+ @model.instance_variable_get(:@name).should == "MockableModel_1"
29
11
  end
30
- it "should say it is not marked_for_destruction" do
31
- @model.marked_for_destruction?.should be(false)
12
+ it "returns string of id value for to_param" do
13
+ @model.to_param.should == "1"
32
14
  end
33
15
  end
34
16
 
35
17
  describe "with params" do
36
- it "should not mutate its parameters" do
18
+ it "does not mutate its parameters" do
37
19
  params = {:a => 'b'}
38
20
  model = mock_model(MockableModel, params)
39
21
  params.should == {:a => 'b'}
40
22
  end
41
23
  end
42
24
 
43
- describe "with #id stubbed", :type => :view do
25
+ describe "as association" do
44
26
  before(:each) do
45
- @model = mock_model(MockableModel, :id => 1)
27
+ @real = AssociatedModel.create!
28
+ @mock_model = mock_model(MockableModel)
29
+ @real.mockable_model = @mock_model
46
30
  end
47
- it "should be named using the stubbed id value" do
48
- @model.instance_variable_get(:@name).should == "MockableModel_1"
31
+
32
+ it "passes: associated_model == mock" do
33
+ @mock_model.should == @real.mockable_model
49
34
  end
50
- it "should return string of id value for to_param" do
51
- @model.to_param.should == "1"
35
+
36
+ it "passes: mock == associated_model" do
37
+ @real.mockable_model.should == @mock_model
38
+ end
39
+ end
40
+
41
+ describe "#is_a?" do
42
+ before(:each) do
43
+ @model = mock_model(SubMockableModel)
44
+ end
45
+ it "says it is_a?(RealModel)" do
46
+ @model.is_a?(SubMockableModel).should be(true)
47
+ end
48
+ it "says it is_a?(OtherModel) if RealModel is an ancestors" do
49
+ @model.is_a?(MockableModel).should be(true)
50
+ end
51
+ end
52
+
53
+ describe "#kind_of?" do
54
+ before(:each) do
55
+ @model = mock_model(SubMockableModel)
56
+ end
57
+ it "says it is kind_of? if RealModel is" do
58
+ @model.kind_of?(SubMockableModel).should be(true)
59
+ end
60
+ it "says it is kind_of? if RealModel's ancestor is" do
61
+ @model.kind_of?(MockableModel).should be(true)
52
62
  end
53
63
  end
54
64
 
55
- context "with id nil" do
56
- it "is not persisted" do
57
- mock_model(MockableModel, :id => nil).should_not be_persisted
65
+ describe "#instance_of?" do
66
+ before(:each) do
67
+ @model = mock_model(SubMockableModel)
68
+ end
69
+ it "says it is instance_of? if RealModel is" do
70
+ @model.instance_of?(SubMockableModel).should be(true)
71
+ end
72
+ it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
73
+ @model.instance_of?(MockableModel).should be(false)
58
74
  end
59
75
  end
60
76
 
61
- describe "valid?" do
77
+ describe "#destroyed?" do
62
78
  context "default" do
63
- it "returns true" do
64
- mock_model(MockableModel).should be_valid
79
+ it "returns false" do
80
+ @model = mock_model(SubMockableModel)
81
+ @model.destroyed?.should be(false)
65
82
  end
66
83
  end
67
- context "stubbed with false" do
84
+ end
85
+
86
+ describe "#marked_for_destruction?" do
87
+ context "default" do
68
88
  it "returns false" do
69
- mock_model(MockableModel, :valid? => false).should_not be_valid
89
+ @model = mock_model(SubMockableModel)
90
+ @model.marked_for_destruction?.should be(false)
70
91
  end
71
92
  end
72
93
  end
73
94
 
74
- describe "as association", :type => :view do
75
- before(:each) do
76
- @real = AssociatedModel.create!
77
- @mock_model = mock_model(MockableModel)
78
- @real.mockable_model = @mock_model
95
+ describe "#persisted?" do
96
+ context "with default id" do
97
+ it "returns true" do
98
+ mock_model(MockableModel).should be_persisted
99
+ end
79
100
  end
80
101
 
81
- it "should pass associated_model == mock" do
82
- @mock_model.should == @real.mockable_model
102
+ context "with explicit id" do
103
+ it "returns true" do
104
+ mock_model(MockableModel, :id => 37).should be_persisted
105
+ end
83
106
  end
84
107
 
85
- it "should pass mock == associated_model" do
86
- @real.mockable_model.should == @mock_model
108
+ context "with id nil" do
109
+ it "returns false" do
110
+ mock_model(MockableModel, :id => nil).should_not be_persisted
111
+ end
87
112
  end
88
113
  end
89
114
 
90
- describe "#as_null_object" do
91
- before(:each) do
92
- @model = mock_model(MockableModel, :mocked_method => "mocked").as_null_object
93
- end
94
115
 
95
- it "should be able to mock methods" do
96
- @model.mocked_method.should == "mocked"
116
+ describe "#valid?" do
117
+ context "default" do
118
+ it "returns true" do
119
+ mock_model(MockableModel).should be_valid
120
+ end
97
121
  end
98
- it "should return itself to unmocked methods" do
99
- @model.unmocked_method.should equal(@model)
122
+ context "stubbed with false" do
123
+ it "returns false" do
124
+ mock_model(MockableModel, :valid? => false).should_not be_valid
125
+ end
100
126
  end
101
127
  end
102
128
 
103
129
  describe "#as_new_record" do
104
- it "should say it is a new record" do
130
+ it "says it is a new record" do
105
131
  m = mock_model(MockableModel)
106
132
  m.as_new_record.should be_new_record
107
133
  end
108
134
 
109
- it "should have a nil id" do
135
+ it "has a nil id" do
110
136
  mock_model(MockableModel).as_new_record.id.should be(nil)
111
137
  end
112
138
 
113
- it "should return nil for #to_param" do
139
+ it "returns nil for #to_param" do
114
140
  mock_model(MockableModel).as_new_record.to_param.should be(nil)
115
141
  end
116
142
  end
@@ -121,7 +147,8 @@ describe "mock_model" do
121
147
  include Test::Unit::Assertions
122
148
  include ActiveModel::Lint::Tests
123
149
 
124
- ActiveModel::Lint::Tests.public_instance_methods.grep(/^test/).each do |m|
150
+ # to_s is to support ruby-1.9
151
+ ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
125
152
  example m.gsub('_',' ') do
126
153
  send m
127
154
  end
@@ -133,7 +160,3 @@ describe "mock_model" do
133
160
 
134
161
  end
135
162
  end
136
-
137
-
138
-
139
-