rspec-rails 2.11.4 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/Capybara.md +2 -2
  2. data/Changelog.md +25 -1
  3. data/README.md +61 -25
  4. data/features/controller_specs/anonymous_controller.feature +25 -25
  5. data/features/controller_specs/bypass_rescue.feature +4 -4
  6. data/features/controller_specs/controller_spec.feature +4 -4
  7. data/features/controller_specs/isolation_from_views.feature +13 -13
  8. data/features/controller_specs/render_views.feature +11 -11
  9. data/features/feature_specs/feature_spec.feature +34 -0
  10. data/features/helper_specs/helper_spec.feature +10 -10
  11. data/features/mailer_specs/url_helpers.feature +7 -7
  12. data/features/matchers/new_record_matcher.feature +20 -7
  13. data/features/matchers/redirect_to_matcher.feature +6 -6
  14. data/features/matchers/relation_match_array.feature +8 -6
  15. data/features/matchers/render_template_matcher.feature +25 -4
  16. data/features/mocks/mock_model.feature +21 -21
  17. data/features/mocks/stub_model.feature +8 -8
  18. data/features/model_specs/errors_on.feature +25 -7
  19. data/features/model_specs/transactional_examples.feature +13 -13
  20. data/features/request_specs/request_spec.feature +8 -8
  21. data/features/routing_specs/be_routable_matcher.feature +10 -10
  22. data/features/routing_specs/named_routes.feature +3 -3
  23. data/features/routing_specs/route_to_matcher.feature +17 -17
  24. data/features/view_specs/inferred_controller_path.feature +7 -6
  25. data/features/view_specs/stub_template.feature +5 -5
  26. data/features/view_specs/view_spec.feature +50 -18
  27. data/lib/autotest/rails_rspec2.rb +1 -1
  28. data/lib/generators/rspec.rb +6 -0
  29. data/lib/generators/rspec/controller/templates/controller_spec.rb +2 -0
  30. data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -0
  31. data/lib/generators/rspec/mailer/templates/mailer_spec.rb +2 -0
  32. data/lib/generators/rspec/model/templates/model_spec.rb +2 -0
  33. data/lib/generators/rspec/observer/templates/observer_spec.rb +2 -0
  34. data/lib/generators/rspec/scaffold/scaffold_generator.rb +37 -3
  35. data/lib/generators/rspec/scaffold/templates/controller_spec.rb +9 -7
  36. data/lib/generators/rspec/scaffold/templates/routing_spec.rb +2 -0
  37. data/lib/rspec/rails/example.rb +5 -1
  38. data/lib/rspec/rails/example/feature_example_group.rb +30 -0
  39. data/lib/rspec/rails/example/view_example_group.rb +23 -12
  40. data/lib/rspec/rails/extensions.rb +1 -0
  41. data/lib/rspec/rails/extensions/active_record/base.rb +5 -3
  42. data/lib/rspec/rails/extensions/active_record/proxy.rb +17 -0
  43. data/lib/rspec/rails/matchers/be_new_record.rb +8 -0
  44. data/lib/rspec/rails/tasks/rspec.rake +1 -2
  45. data/lib/rspec/rails/vendor/capybara.rb +2 -8
  46. data/lib/rspec/rails/version.rb +1 -1
  47. data/spec/autotest/rails_rspec2_spec.rb +4 -4
  48. data/spec/generators/rspec/model/model_generator_spec.rb +1 -1
  49. data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +8 -0
  50. data/spec/rspec/rails/example/feature_example_group_spec.rb +56 -0
  51. data/spec/rspec/rails/example/view_example_group_spec.rb +7 -1
  52. data/spec/rspec/rails/matchers/be_new_record_spec.rb +16 -2
  53. data/spec/rspec/rails/matchers/has_spec.rb +29 -0
  54. data/spec/rspec/rails/matchers/relation_match_array_spec.rb +18 -7
  55. metadata +66 -10
@@ -1,19 +1,21 @@
1
1
  Feature: ActiveRecord::Relation match array
2
2
 
3
- The `=~` matcher can be used with an `ActiveRecord::Relation` (scope). The
4
- assertion will pass if the scope would return all of the elements specified
5
- in the array on the right hand side.
3
+ The `match_array` matcher can be used with an `ActiveRecord::Relation`
4
+ (scope). The assertion will pass if the scope would return all of the
5
+ elements specified in the array on the right hand side.
6
6
 
7
- Scenario: example spec with relation =~ matcher
7
+ Scenario: example spec with relation match_array matcher
8
8
  Given a file named "spec/models/widget_spec.rb" with:
9
- """
9
+ """ruby
10
10
  require "spec_helper"
11
11
 
12
12
  describe Widget do
13
13
  let!(:widgets) { Array.new(3) { Widget.create } }
14
14
  subject { Widget.scoped }
15
15
 
16
- it { should =~ widgets }
16
+ it "returns all widgets in any order" do
17
+ expect(subject).to match_array(widgets)
18
+ end
17
19
  end
18
20
  """
19
21
  When I run `rspec spec/models/widget_spec.rb`
@@ -11,18 +11,39 @@ Feature: render_template matcher
11
11
 
12
12
  Scenario: render_template with three possible options
13
13
  Given a file named "spec/controllers/gadgets_spec.rb" with:
14
- """
14
+ """ruby
15
15
  require "spec_helper"
16
16
 
17
17
  describe GadgetsController do
18
18
  describe "GET #index" do
19
19
  subject { get :index }
20
20
 
21
- it { should render_template(:index) }
22
- it { should render_template("index") }
23
- it { should render_template("gadgets/index") }
21
+ it "renders the index template" do
22
+ expect(subject).to render_template(:index)
23
+ expect(subject).to render_template("index")
24
+ expect(subject).to render_template("gadgets/index")
25
+ end
24
26
  end
25
27
  end
26
28
  """
27
29
  When I run `rspec spec/controllers/gadgets_spec.rb`
28
30
  Then the examples should all pass
31
+
32
+ Scenario: render_template in a view spec
33
+ Given a file named "spec/views/gadgets/index.html.erb_spec.rb" with:
34
+ """ruby
35
+ require "spec_helper"
36
+
37
+ describe "gadgets/index" do
38
+ it "renders the index template" do
39
+ assign(:gadgets, [stub_model(Gadget)])
40
+ render
41
+
42
+ expect(view).to render_template(:index)
43
+ expect(view).to render_template("index")
44
+ expect(view).to render_template("gadgets/index")
45
+ end
46
+ end
47
+ """
48
+ When I run `rspec spec/views`
49
+ Then the examples should all pass
@@ -13,25 +13,25 @@ Feature: mock_model
13
13
 
14
14
  Scenario: passing a string that represents a non-existent constant
15
15
  Given a file named "spec/models/car_spec.rb" with:
16
- """
16
+ """ruby
17
17
  require "spec_helper"
18
18
 
19
19
  describe "mock_model('Car') with no Car constant in existence" do
20
20
  it "generates a constant" do
21
- Object.const_defined?(:Car).should be_false
21
+ expect(Object.const_defined?(:Car)).to be_false
22
22
  mock_model("Car")
23
- Object.const_defined?(:Car).should be_true
23
+ expect(Object.const_defined?(:Car)).to be_true
24
24
  end
25
25
 
26
26
  describe "generates an object that ..." do
27
27
  it "returns the correct name" do
28
28
  car = mock_model("Car")
29
- car.class.name.should eq("Car")
29
+ expect(car.class.name).to eq("Car")
30
30
  end
31
31
 
32
32
  it "says it is a Car" do
33
33
  car = mock_model("Car")
34
- car.should be_a(Car)
34
+ expect(car).to be_a(Car)
35
35
  end
36
36
  end
37
37
  end
@@ -41,13 +41,13 @@ Feature: mock_model
41
41
 
42
42
  Scenario: passing a string that represents an existing constant
43
43
  Given a file named "spec/models/widget_spec.rb" with:
44
- """
44
+ """ruby
45
45
  require "spec_helper"
46
46
 
47
47
  describe Widget do
48
48
  it "uses the existing constant" do
49
49
  widget = mock_model("Widget")
50
- widget.should be_a(Widget)
50
+ expect(widget).to be_a(Widget)
51
51
  end
52
52
  end
53
53
  """
@@ -56,7 +56,7 @@ Feature: mock_model
56
56
 
57
57
  Scenario: passing a class that does not extend ActiveModel::Naming
58
58
  Given a file named "spec/models/string_spec.rb" with:
59
- """
59
+ """ruby
60
60
  require "spec_helper"
61
61
 
62
62
  describe String do
@@ -70,27 +70,27 @@ Feature: mock_model
70
70
 
71
71
  Scenario: passing an Active Record constant
72
72
  Given a file named "spec/models/widget_spec.rb" with:
73
- """
73
+ """ruby
74
74
  require "spec_helper"
75
75
 
76
76
  describe Widget do
77
77
  let(:widget) { mock_model(Widget) }
78
78
 
79
79
  it "is valid by default" do
80
- widget.should be_valid
80
+ expect(widget).to be_valid
81
81
  end
82
82
 
83
83
  it "is not a new record by default" do
84
- widget.should_not be_new_record
84
+ expect(widget).not_to be_new_record
85
85
  end
86
86
 
87
87
  it "can be converted to a new record" do
88
- widget.as_new_record.should be_new_record
88
+ expect(widget.as_new_record).to be_new_record
89
89
  end
90
90
 
91
91
  it "sets :id to nil upon destroy" do
92
92
  widget.destroy
93
- widget.id.should be_nil
93
+ expect(widget.id).to be_nil
94
94
  end
95
95
  end
96
96
  """
@@ -99,7 +99,7 @@ Feature: mock_model
99
99
 
100
100
  Scenario: passing an Active Record constant with method stubs
101
101
  Given a file named "spec/models/widget_spec.rb" with:
102
- """
102
+ """ruby
103
103
  require "spec_helper"
104
104
 
105
105
  describe "mock_model(Widget) with stubs" do
@@ -110,18 +110,18 @@ Feature: mock_model
110
110
  end
111
111
 
112
112
  it "supports stubs for methods that don't exist in ActiveModel or ActiveRecord" do
113
- widget.foo.should eq("bar")
113
+ expect(widget.foo).to eq("bar")
114
114
  end
115
115
 
116
116
  it "supports stubs for methods that do exist" do
117
- widget.save.should eq(true)
118
- widget.update_attributes.should be_false
117
+ expect(widget.save).to eq(true)
118
+ expect(widget.update_attributes).to be_false
119
119
  end
120
120
 
121
121
  describe "#errors" do
122
122
  context "with update_attributes => false" do
123
123
  it "is not empty" do
124
- widget.errors.should_not be_empty
124
+ expect(widget.errors).not_to be_empty
125
125
  end
126
126
  end
127
127
  end
@@ -132,14 +132,14 @@ Feature: mock_model
132
132
 
133
133
  Scenario: mock_model outside rails
134
134
  Given a file named "mock_model_outside_rails_spec.rb" with:
135
- """
135
+ """ruby
136
136
  require 'rspec/rails/mocks'
137
137
 
138
138
  describe "Foo" do
139
139
  it "is mockable" do
140
140
  foo = mock_model("Foo")
141
- foo.id.should eq(1001)
142
- foo.to_param.should eq("1001")
141
+ expect(foo.id).to eq(1001)
142
+ expect(foo.to_param).to eq("1001")
143
143
  end
144
144
  end
145
145
  """
@@ -8,7 +8,7 @@ Feature: stub_model
8
8
 
9
9
  Scenario: passing an Active Record constant with a hash of stubs
10
10
  Given a file named "spec/models/widget_spec.rb" with:
11
- """
11
+ """ruby
12
12
  require "spec_helper"
13
13
 
14
14
  describe "stub_model(Widget) with a hash of stubs" do
@@ -17,20 +17,20 @@ Feature: stub_model
17
17
  end
18
18
 
19
19
  it "stubs :id" do
20
- widget.id.should eql(5)
20
+ expect(widget.id).to eql(5)
21
21
  end
22
22
 
23
23
  it "stubs :random_attribute" do
24
- widget.random_attribute.should be_true
24
+ expect(widget.random_attribute).to be_true
25
25
  end
26
26
 
27
27
  it "returns false for new_record? if :id is set" do
28
- widget.should_not be_new_record
28
+ expect(widget).not_to be_new_record
29
29
  end
30
30
 
31
31
  it "can be converted to a new record" do
32
32
  widget.as_new_record
33
- widget.should be_new_record
33
+ expect(widget).to be_new_record
34
34
  end
35
35
  end
36
36
  """
@@ -39,7 +39,7 @@ Feature: stub_model
39
39
 
40
40
  Scenario: passing an Active Record constant with a block of stubs
41
41
  Given a file named "spec/models/widget_spec.rb" with:
42
- """
42
+ """ruby
43
43
  require "spec_helper"
44
44
 
45
45
  describe "stub_model(Widget) with a block of stubs" do
@@ -50,9 +50,9 @@ Feature: stub_model
50
50
  end
51
51
 
52
52
  it "stubs :id" do
53
- widget.id.should eql(5)
53
+ expect(widget.id).to eql(5)
54
54
  end
55
55
  end
56
56
  """
57
57
  When I run `rspec spec/models/widget_spec.rb`
58
- Then the examples should all pass
58
+ Then the examples should all pass
@@ -2,30 +2,48 @@ Feature: errors_on
2
2
 
3
3
  Scenario: with one validation error
4
4
  Given a file named "spec/models/widget_spec.rb" with:
5
- """
5
+ """ruby
6
6
  require "spec_helper"
7
7
 
8
8
  class ValidatingWidget < ActiveRecord::Base
9
- set_table_name :widgets
9
+ self.table_name = :widgets
10
10
  validates_presence_of :name
11
- attr_accessible :name
11
+
12
+ # In Rails 4, mass assignment protection is implemented on controllers
13
+ attr_accessible :name if Rails.version < '4'
14
+
15
+ validates_length_of :name, :minimum => 10, :on => :publication
12
16
  end
13
17
 
14
18
  describe ValidatingWidget do
15
19
  it "fails validation with no name (using error_on)" do
16
- ValidatingWidget.new.should have(1).error_on(:name)
20
+ expect(ValidatingWidget.new).to have(1).error_on(:name)
17
21
  end
18
22
 
19
23
  it "fails validation with no name (using errors_on)" do
20
- ValidatingWidget.new.should have(1).errors_on(:name)
24
+ expect(ValidatingWidget.new).to have(1).errors_on(:name)
25
+ end
26
+
27
+ it "fails validation with no name expecting a specific message" do
28
+ expect(ValidatingWidget.new.errors_on(:name)).to include("can't be blank")
29
+ end
30
+
31
+ it "fails validation with a short name (using a validation context)" do
32
+ expect(ValidatingWidget.new(:name => "too short")).
33
+ to have(1).errors_on(:name, :context => :publication)
34
+ end
35
+
36
+ it "passes validation with a longer name (using a validation context)" do
37
+ expect(ValidatingWidget.new(:name => "a longer name")).
38
+ to have(0).errors_on(:name, :context => :publication)
21
39
  end
22
40
 
23
41
  it "passes validation with a name (using 0)" do
24
- ValidatingWidget.new(:name => "liquid nitrogen").should have(0).errors_on(:name)
42
+ expect(ValidatingWidget.new(:name => "liquid nitrogen")).to have(0).errors_on(:name)
25
43
  end
26
44
 
27
45
  it "passes validation with a name (using :no)" do
28
- ValidatingWidget.new(:name => "liquid nitrogen").should have(:no).errors_on(:name)
46
+ expect(ValidatingWidget.new(:name => "liquid nitrogen")).to have(:no).errors_on(:name)
29
47
  end
30
48
  end
31
49
  """
@@ -7,21 +7,21 @@ Feature: transactional examples
7
7
 
8
8
  Scenario: run in transactions (default)
9
9
  Given a file named "spec/models/widget_spec.rb" with:
10
- """
10
+ """ruby
11
11
  require "spec_helper"
12
12
 
13
13
  describe Widget do
14
14
  it "has none to begin with" do
15
- Widget.count.should == 0
15
+ expect(Widget.count).to eq 0
16
16
  end
17
17
 
18
18
  it "has one after adding one" do
19
19
  Widget.create
20
- Widget.count.should == 1
20
+ expect(Widget.count).to eq 1
21
21
  end
22
22
 
23
23
  it "has none after one was created in a previous example" do
24
- Widget.count.should == 0
24
+ expect(Widget.count).to eq 0
25
25
  end
26
26
  end
27
27
  """
@@ -30,7 +30,7 @@ Feature: transactional examples
30
30
 
31
31
  Scenario: run in transactions (explicit)
32
32
  Given a file named "spec/models/widget_spec.rb" with:
33
- """
33
+ """ruby
34
34
  require "spec_helper"
35
35
 
36
36
  RSpec.configure do |c|
@@ -39,16 +39,16 @@ Feature: transactional examples
39
39
 
40
40
  describe Widget do
41
41
  it "has none to begin with" do
42
- Widget.count.should == 0
42
+ expect(Widget.count).to eq 0
43
43
  end
44
44
 
45
45
  it "has one after adding one" do
46
46
  Widget.create
47
- Widget.count.should == 1
47
+ expect(Widget.count).to eq 1
48
48
  end
49
49
 
50
50
  it "has none after one was created in a previous example" do
51
- Widget.count.should == 0
51
+ expect(Widget.count).to eq 0
52
52
  end
53
53
  end
54
54
  """
@@ -57,7 +57,7 @@ Feature: transactional examples
57
57
 
58
58
  Scenario: disable transactions (explicit)
59
59
  Given a file named "spec/models/widget_spec.rb" with:
60
- """
60
+ """ruby
61
61
  require "spec_helper"
62
62
 
63
63
  RSpec.configure do |c|
@@ -67,16 +67,16 @@ Feature: transactional examples
67
67
 
68
68
  describe Widget do
69
69
  it "has none to begin with" do
70
- Widget.count.should == 0
70
+ expect(Widget.count).to eq 0
71
71
  end
72
72
 
73
73
  it "has one after adding one" do
74
74
  Widget.create
75
- Widget.count.should == 1
75
+ expect(Widget.count).to eq 1
76
76
  end
77
77
 
78
78
  it "has one after one was created in a previous example" do
79
- Widget.count.should == 1
79
+ expect(Widget.count).to eq 1
80
80
  end
81
81
 
82
82
  after(:all) { Widget.destroy_all }
@@ -87,7 +87,7 @@ Feature: transactional examples
87
87
 
88
88
  Scenario: run in transactions with fixture
89
89
  Given a file named "spec/models/thing_spec.rb" with:
90
- """
90
+ """ruby
91
91
  require "spec_helper"
92
92
 
93
93
  describe Thing do
@@ -19,28 +19,28 @@ Feature: request spec
19
19
 
20
20
  Check the Rails docs for details on these methods as well.
21
21
 
22
- If you would like to use webrat or capybara with your request specs, all you
23
- have to do is include one of them in your Gemfile and RSpec will
24
- automatically load them in a request spec.
22
+ [Capybara](http://github.com/jnicklas/capybara) is no longer supported in
23
+ request specs as of Capybara 2.0.0. The recommended way to use Capybara is
24
+ with [feature specs](../feature_specs/feature_spec.feature).
25
25
 
26
26
  Scenario: specify managing a Widget with Rails integration methods
27
27
  Given a file named "spec/requests/widget_management_spec.rb" with:
28
- """
28
+ """ruby
29
29
  require "spec_helper"
30
30
 
31
31
  describe "Widget management" do
32
32
 
33
33
  it "creates a Widget and redirects to the Widget's page" do
34
34
  get "/widgets/new"
35
- response.should render_template(:new)
35
+ expect(response).to render_template(:new)
36
36
 
37
37
  post "/widgets", :widget => {:name => "My Widget"}
38
38
 
39
- response.should redirect_to(assigns(:widget))
39
+ expect(response).to redirect_to(assigns(:widget))
40
40
  follow_redirect!
41
41
 
42
- response.should render_template(:show)
43
- response.body.should include("Widget was successfully created.")
42
+ expect(response).to render_template(:show)
43
+ expect(response.body).to include("Widget was successfully created.")
44
44
  end
45
45
 
46
46
  end
@@ -6,12 +6,12 @@ Feature: be_routable matcher
6
6
 
7
7
  Scenario: specify routeable route should not be routable (fails)
8
8
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
9
- """
9
+ """ruby
10
10
  require "spec_helper"
11
11
 
12
12
  describe "routes for Widgets" do
13
13
  it "does not route to widgets" do
14
- { :get => "/widgets" }.should_not be_routable
14
+ expect(:get => "/widgets").not_to be_routable
15
15
  end
16
16
  end
17
17
  """
@@ -21,12 +21,12 @@ Feature: be_routable matcher
21
21
 
22
22
  Scenario: specify non-routeable route should not be routable (passes)
23
23
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
24
- """
24
+ """ruby
25
25
  require "spec_helper"
26
26
 
27
27
  describe "routes for Widgets" do
28
28
  it "does not route to widgets/foo/bar" do
29
- { :get => "/widgets/foo/bar" }.should_not be_routable
29
+ expect(:get => "/widgets/foo/bar").not_to be_routable
30
30
  end
31
31
  end
32
32
  """
@@ -36,12 +36,12 @@ Feature: be_routable matcher
36
36
 
37
37
  Scenario: specify routeable route should be routable (passes)
38
38
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
39
- """
39
+ """ruby
40
40
  require "spec_helper"
41
41
 
42
42
  describe "routes for Widgets" do
43
43
  it "routes to /widgets" do
44
- { :get => "/widgets" }.should be_routable
44
+ expect(:get => "/widgets").to be_routable
45
45
  end
46
46
  end
47
47
  """
@@ -51,12 +51,12 @@ Feature: be_routable matcher
51
51
 
52
52
  Scenario: specify non-routeable route should be routable (fails)
53
53
  Given a file named "spec/routing/widgets_routing_spec.rb" with:
54
- """
54
+ """ruby
55
55
  require "spec_helper"
56
56
 
57
57
  describe "routes for Widgets" do
58
58
  it "routes to widgets/foo/bar" do
59
- { :get => "/widgets/foo/bar" }.should be_routable
59
+ expect(:get => "/widgets/foo/bar").to be_routable
60
60
  end
61
61
  end
62
62
  """
@@ -66,12 +66,12 @@ Feature: be_routable matcher
66
66
 
67
67
  Scenario: be_routable in a controller spec
68
68
  Given a file named "spec/controllers/widgets_controller_spec.rb" with:
69
- """
69
+ """ruby
70
70
  require "spec_helper"
71
71
 
72
72
  describe WidgetsController do
73
73
  it "routes to /widgets" do
74
- { :get => "/widgets" }.should be_routable
74
+ expect(:get => "/widgets").to be_routable
75
75
  end
76
76
  end
77
77
  """