rspec-subject-extensions 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,13 +2,14 @@
2
2
 
3
3
  rspec-subject-extensions adds `each` short-hand to generate a nested example group with
4
4
  a single example that specifies the expected value of each attribute of the subject.
5
+ If no attribute is given, each element of the subject will be used.
5
6
 
6
7
  ## Documentation
7
8
 
8
9
  The [Cucumber features](http://relishapp.com/ZenCocoon/rspec-subject-extensions)
9
10
  are the most comprehensive and up-to-date docs for end-users.
10
11
 
11
- The [RDoc](http://rubydoc.info/gems/rspec-subject-extensions/0.2.3/frames) provides
12
+ The [RDoc](http://rubydoc.info/gems/rspec-subject-extensions/0.3.0/frames) provides
12
13
  additional information for contributors and/or extenders.
13
14
 
14
15
  All of the documentation is open source and a work in progress. If you find it
@@ -30,6 +31,8 @@ tracker](https://github.com/ZenCocoon/rspec-subject-extensions/issues).
30
31
 
31
32
  ### Each
32
33
 
34
+ #### Using the singular name of the attributes
35
+
33
36
  Creates a nested example group named by `each` and the submitted `attribute`,
34
37
  and then generates an example for each attribute using the submitted block.
35
38
 
@@ -51,6 +54,28 @@ and then generates an example for each attribute using the submitted block.
51
54
 
52
55
  The `attribute` can be a `Symbol` or a `String`.
53
56
 
57
+ #### Using no attribute. Ideal to test scopes
58
+
59
+ Creates a nested example group and then generates an example
60
+ for each instance using the submitted block.
61
+
62
+ # This ...
63
+ describe Object do
64
+ subject { Object.visible }
65
+ each { should be_visible }
66
+ end
67
+
68
+ # ... generates the same runtime structure as this:
69
+ describe Object do
70
+ describe "each instance" do
71
+ it "should be visible" do
72
+ subject.each do |element|
73
+ element.should be_visible
74
+ end
75
+ end
76
+ end
77
+ end
78
+
54
79
  ## Also see
55
80
 
56
81
  * [http://github.com/rspec/rspec](http://github.com/rspec/rspec)
@@ -1,9 +1,12 @@
1
1
  rspec-subject-extensions adds `each` short-hand to generate a nested example group with
2
2
  a single example that specifies the expected value of each attribute of the subject.
3
+ If no attribute is given, each element of the subject will be used.
3
4
 
4
5
  ## Each
5
6
 
6
- Creates a nested example group named by +each+ and the submitted +attribute+,
7
+ #### Using the singular name of the attributes
8
+
9
+ Creates a nested example group named by `each` and the submitted `attribute`,
7
10
  and then generates an example for each attribute using the submitted block.
8
11
 
9
12
  # This ...
@@ -22,7 +25,29 @@ and then generates an example for each attribute using the submitted block.
22
25
  end
23
26
  end
24
27
 
25
- The +attribute+ can be a +Symbol+ or a +String+.
28
+ The `attribute` can be a `Symbol` or a `String`.
29
+
30
+ #### Using no attribute. Ideal to test scopes
31
+
32
+ Creates a nested example group and then generates an example
33
+ for each instance using the submitted block.
34
+
35
+ # This ...
36
+ describe Object do
37
+ subject { Object.visible }
38
+ each { should be_visible }
39
+ end
40
+
41
+ # ... generates the same runtime structure as this:
42
+ describe Object do
43
+ describe "each instance" do
44
+ it "should be visible" do
45
+ subject.each do |element|
46
+ element.should be_visible
47
+ end
48
+ end
49
+ end
50
+ end
26
51
 
27
52
  ## Issues
28
53
 
@@ -0,0 +1,33 @@
1
+ Feature: each element of subject
2
+
3
+ Use the each() method as a short-hand to generate a nested example group with
4
+ a single example that specifies the expected value of each element of the
5
+ subject. This can be used with an implicit or explicit subject.
6
+
7
+ each() requires a block representing the example.
8
+
9
+ each { should be_visible }
10
+
11
+ Scenario: specify value of each element
12
+ Given a file named "example_spec.rb" with:
13
+ """
14
+ require 'rspec-subject-extensions'
15
+
16
+ class Post
17
+ def visible?
18
+ true
19
+ end
20
+ end
21
+
22
+ describe Post do
23
+ subject { [Post.new, Post.new] }
24
+ each { should be_visible }
25
+ end
26
+ """
27
+ When I run `rspec example_spec.rb --format documentation`
28
+ Then the output should contain:
29
+ """
30
+ Post
31
+ each instance
32
+ should be visible
33
+ """
@@ -3,18 +3,19 @@ require 'active_support/inflector'
3
3
  module RSpecSubjectExtensions
4
4
  module ClassMethods
5
5
  # Creates a nested example group named by +each+ and the submitted +attribute+,
6
- # and then generates an example for each attribute using the submitted block.
6
+ # or the +instances+ themselves if none given, and then generates an example for
7
+ # each of them using the submitted block.
7
8
  #
8
- # @param [Symbol, String] attribute
9
+ # @param optional [Symbol, String] attribute
9
10
  # The singular name of the subject method containing all the attributes.
10
11
  #
11
12
  # @yield
12
- # Example to run against each attribute.
13
+ # Example to run against each attribute (or instance if no attribute given).
13
14
  #
14
15
  # @raise [NoMethodError]
15
16
  # The subject doesn't respond to the pluralized version of the attribute or it doesn't respond to each.
16
17
  #
17
- # @example
18
+ # @example Using the singular name of the attributes
18
19
  # # This ...
19
20
  # describe Object do
20
21
  # each(:item) { should be_an(Integer) }
@@ -30,13 +31,38 @@ module RSpecSubjectExtensions
30
31
  # end
31
32
  # end
32
33
  # end
33
- def each(attribute, &block)
34
- describe("each #{attribute}") do
35
- attribute = attribute.to_s.pluralize
34
+ #
35
+ # @example Using no attribute. Ideal to test scopes
36
+ # # This ...
37
+ # describe Object do
38
+ # subject { Object.visible }
39
+ # each { should be_visible }
40
+ # end
41
+ #
42
+ # # ... generates the same runtime structure as this:
43
+ # describe Object do
44
+ # describe "each instance" do
45
+ # it "should be visible" do
46
+ # subject.each do |element|
47
+ # element.should be_visible
48
+ # end
49
+ # end
50
+ # end
51
+ # end
52
+ def each(attribute = nil, &block)
53
+ if attribute
54
+ each_with_attribute(attribute, &block)
55
+ else
56
+ each_without_attribute(&block)
57
+ end
58
+ end
59
+
60
+ private
36
61
 
37
- example do
38
- if subject.respond_to?(attribute) && subject.send(attribute).respond_to?(:each)
39
- subject.send(attribute).each do |item|
62
+ def each_without_attribute(&block)
63
+ describe("each instance") do
64
+ example do
65
+ subject.each do |item|
40
66
  self.class.class_eval do
41
67
  define_method(:subject) do
42
68
  @_subject = item
@@ -45,18 +71,36 @@ module RSpecSubjectExtensions
45
71
 
46
72
  instance_eval(&block)
47
73
  end
48
- else
49
- self.class.class_eval do
50
- define_method(:subject) do
51
- @_subject ||= super().send(attribute).each
74
+ end
75
+ end
76
+ end
77
+
78
+ def each_with_attribute(attribute, &block)
79
+ describe("each #{attribute}") do
80
+ attribute = attribute.to_s.pluralize
81
+
82
+ example do
83
+ if subject.respond_to?(attribute) && subject.send(attribute).respond_to?(:each)
84
+ subject.send(attribute).each do |item|
85
+ self.class.class_eval do
86
+ define_method(:subject) do
87
+ @_subject = item
88
+ end
89
+ end
90
+
91
+ instance_eval(&block)
92
+ end
93
+ else
94
+ self.class.class_eval do
95
+ define_method(:subject) do
96
+ @_subject ||= super().send(attribute).each
97
+ end
52
98
  end
53
- end
54
99
 
55
- instance_eval(&block)
100
+ instance_eval(&block)
101
+ end
56
102
  end
57
103
  end
58
104
  end
59
- end
60
-
61
105
  end
62
106
  end
@@ -1,6 +1,6 @@
1
1
  module RSpecSubjectExtensions
2
2
  # @private
3
3
  module Version
4
- STRING = '0.2.3'
4
+ STRING = '0.3.0'
5
5
  end
6
6
  end
@@ -8,63 +8,95 @@ module RSpecSubjectExtensions::ClassMethods
8
8
  end
9
9
 
10
10
  describe "#each" do
11
- it "should satisfy expectation" do
12
- group = RSpec::Core::ExampleGroup.describe("object") do
13
- subject {
14
- Class.new do
15
- def items
16
- [1, 2]
17
- end
18
- end.new
19
- }
11
+ context "with attribute" do
12
+ it "should satisfy expectation" do
13
+ group = RSpec::Core::ExampleGroup.describe("object") do
14
+ subject {
15
+ Class.new do
16
+ def items
17
+ [1, 2]
18
+ end
19
+ end.new
20
+ }
20
21
 
21
- each(:item) { should be_an(Integer) }
22
+ each(:item) { should be_an(Integer) }
23
+ end
24
+ group.run(NullObject.new).should be_true
22
25
  end
23
- group.run(NullObject.new).should be_true
24
- end
25
26
 
26
- it "fails when expectation should fail" do
27
- group = RSpec::Core::ExampleGroup.describe("object") do
28
- subject {
27
+ it "fails when expectation should fail" do
28
+ group = RSpec::Core::ExampleGroup.describe("object") do
29
+ subject {
30
+ Class.new do
31
+ def items
32
+ [1, 'a']
33
+ end
34
+ end.new
35
+ }
36
+
37
+ each(:item) { should be_an(Integer) }
38
+ end
39
+ group.run(NullObject.new).should be_false
40
+ end
41
+
42
+ context "when it doesn't respond to the pluralized version of the attribute" do
43
+ subject { Object.new }
44
+
45
+ context "it raises an error" do
46
+ each(:item) do
47
+ expect do
48
+ should be_an(Integer)
49
+ end.to raise_error(NoMethodError)
50
+ end
51
+ end
52
+ end
53
+
54
+ context "when it doesn't return an object responding to each" do
55
+ subject do
29
56
  Class.new do
30
57
  def items
31
- [1, 'a']
58
+ 1
32
59
  end
33
60
  end.new
34
- }
61
+ end
35
62
 
36
- each(:item) { should be_an(Integer) }
63
+ context "it raises an error" do
64
+ each(:item) do
65
+ expect do
66
+ should be_an(Integer)
67
+ end.to raise_error(NoMethodError)
68
+ end
69
+ end
37
70
  end
38
- group.run(NullObject.new).should be_false
39
71
  end
40
72
 
41
- context "when it doesn't respond to the pluralized version of the attribute" do
42
- subject { Object.new }
43
-
44
- context "it raises an error" do
45
- each(:item) do
46
- expect do
47
- should be_an(Integer)
48
- end.to raise_error(NoMethodError)
73
+ context "without attribute" do
74
+ it "should satisfy expectation" do
75
+ group = RSpec::Core::ExampleGroup.describe("object") do
76
+ subject { [1, 2] }
77
+ each { should be_an(Integer) }
49
78
  end
79
+ group.run(NullObject.new).should be_true
50
80
  end
51
- end
52
81
 
53
- context "when it doesn't return an object responding to each" do
54
- subject do
55
- Class.new do
56
- def items
57
- 1
58
- end
59
- end.new
82
+ it "fails when expectation should fail" do
83
+ group = RSpec::Core::ExampleGroup.describe("object") do
84
+ subject { [1, 'a'] }
85
+ each { should be_an(Integer) }
86
+ end
87
+ group.run(NullObject.new).should be_false
60
88
  end
61
89
 
62
- context "it raises an error" do
63
- each(:item) do
64
- expect do
65
- should be_an(Integer)
66
- end.to raise_error(NoMethodError)
90
+ it "fails when the object doesn't respond to each" do
91
+ group = RSpec::Core::ExampleGroup.describe("object") do
92
+ subject { 1 }
93
+ each do
94
+ expect {
95
+ should be_an(Integer)
96
+ }.to raise_error(NoMethodError)
97
+ end
67
98
  end
99
+ group.run(NullObject.new).should be_false
68
100
  end
69
101
  end
70
102
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-subject-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70234557744340 !ruby/object:Gem::Requirement
16
+ requirement: &70194559952160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.6.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70234557744340
24
+ version_requirements: *70194559952160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &70234557743520 !ruby/object:Gem::Requirement
27
+ requirement: &70194559951340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.5.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70234557743520
35
+ version_requirements: *70194559951340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &70234557742860 !ruby/object:Gem::Requirement
38
+ requirement: &70194559950560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70234557742860
46
+ version_requirements: *70194559950560
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &70234557741700 !ruby/object:Gem::Requirement
49
+ requirement: &70194559949620 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0.9'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70234557741700
57
+ version_requirements: *70194559949620
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: cucumber
60
- requirement: &70234557737780 !ruby/object:Gem::Requirement
60
+ requirement: &70194559947720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - =
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70234557737780
68
+ version_requirements: *70194559947720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: aruba
71
- requirement: &70234557736440 !ruby/object:Gem::Requirement
71
+ requirement: &70194559946760 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - =
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.4.2
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70234557736440
79
+ version_requirements: *70194559946760
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: nokogiri
82
- requirement: &70234557735760 !ruby/object:Gem::Requirement
82
+ requirement: &70194559945980 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - =
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.4.4
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70234557735760
90
+ version_requirements: *70194559945980
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: relish
93
- requirement: &70234557734960 !ruby/object:Gem::Requirement
93
+ requirement: &70194559944660 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - =
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: 0.4.0
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70234557734960
101
+ version_requirements: *70194559944660
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: yard
104
- requirement: &70234557734120 !ruby/object:Gem::Requirement
104
+ requirement: &70194559943600 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ~>
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 0.7.2
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70234557734120
112
+ version_requirements: *70194559943600
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: guard-rspec
115
- requirement: &70234557733300 !ruby/object:Gem::Requirement
115
+ requirement: &70194559942900 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - =
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: 0.1.9
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70234557733300
123
+ version_requirements: *70194559942900
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: guard-cucumber
126
- requirement: &70234557732500 !ruby/object:Gem::Requirement
126
+ requirement: &70194559942180 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ~>
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: 0.5.1
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *70234557732500
134
+ version_requirements: *70194559942180
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: growl
137
- requirement: &70234557731560 !ruby/object:Gem::Requirement
137
+ requirement: &70194559941520 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - =
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: 1.0.3
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *70234557731560
145
+ version_requirements: *70194559941520
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: appraisal
148
- requirement: &70234557725180 !ruby/object:Gem::Requirement
148
+ requirement: &70194559940720 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ~>
@@ -153,7 +153,7 @@ dependencies:
153
153
  version: 0.3.7
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *70234557725180
156
+ version_requirements: *70194559940720
157
157
  description: rspec-subject-extensions let's you use short-hands to generate nested
158
158
  examples groups
159
159
  email: public@zencocoon.com
@@ -168,6 +168,7 @@ files:
168
168
  - README.md
169
169
  - features/README.md
170
170
  - features/class_methods/each_attribute_of_subject.feature
171
+ - features/class_methods/each_element_of_subject.feature
171
172
  - features/support/env.rb
172
173
  - spec/rspec-subject-extensions/class_methods_spec.rb
173
174
  - spec/spec_helper.rb
@@ -195,10 +196,11 @@ rubyforge_project:
195
196
  rubygems_version: 1.8.11
196
197
  signing_key:
197
198
  specification_version: 3
198
- summary: rspec-subject-extensions-0.2.3
199
+ summary: rspec-subject-extensions-0.3.0
199
200
  test_files:
200
201
  - features/README.md
201
202
  - features/class_methods/each_attribute_of_subject.feature
203
+ - features/class_methods/each_element_of_subject.feature
202
204
  - features/support/env.rb
203
205
  - spec/rspec-subject-extensions/class_methods_spec.rb
204
206
  - spec/spec_helper.rb