Skip to content
This repository
Browse code

Beef up/clarified specs for #582.

Also refactored `its` a bit
  • Loading branch information...
commit a9510100624da7c7a07598784f7ca92b08559412 1 parent 59c5dde
David Chelimsky authored March 17, 2012
23  lib/rspec/core/subject.rb
@@ -65,6 +65,16 @@ def should_not(matcher=nil, message=nil)
65 65
           end
66 66
         rescue LoadError
67 67
         end
  68
+
  69
+        def _attribute_chain(attribute)
  70
+          attribute.to_s.split('.')
  71
+        end
  72
+
  73
+        def _nested_attribute(subject, attribute)
  74
+          _attribute_chain(attribute).inject(subject) do |subject, attr|
  75
+            subject.send(attr)
  76
+          end
  77
+        end
68 78
       end
69 79
 
70 80
       module ExampleGroupMethods
@@ -120,16 +130,11 @@ def its(attribute, &block)
120 130
             example do
121 131
               self.class.class_eval do
122 132
                 define_method(:subject) do
123  
-                  unless instance_variable_defined?(:@_subject)
124  
-                    @_subject = if attribute.is_a?(Array)
125  
-                                  super()[*attribute]
126  
-                                else
127  
-                                  attribute.to_s.split('.').inject(super()) do |target, method|
128  
-                                    target.send(method)
129  
-                                  end
130  
-                                end
  133
+                  if defined?(@_subject)
  134
+                    @_subject
  135
+                  else
  136
+                    @_subject = Array === attribute ? super()[*attribute] : _nested_attribute(super(), attribute)
131 137
                   end
132  
-                  @_subject
133 138
                 end
134 139
               end
135 140
               instance_eval(&block)
29  spec/rspec/core/subject_spec.rb
@@ -196,23 +196,34 @@ def subject; super().first; end
196 196
         end
197 197
       end
198 198
 
199  
-      context "memorize subject" do
  199
+      context "with nil subject" do
200 200
         subject do
201 201
           Class.new do
202 202
             def initialize
203  
-              @counter = 0
  203
+              @counter = -1
  204
+            end
  205
+            def nil_if_first_time
  206
+              @counter += 1
  207
+              @counter == 0 ? nil : true
  208
+            end
  209
+          end.new
  210
+        end
  211
+        its(:nil_if_first_time) { should be(nil) }
  212
+      end
  213
+
  214
+      context "with false subject" do
  215
+        subject do
  216
+          Class.new do
  217
+            def initialize
  218
+              @counter = -1
204 219
             end
205 220
             def false_if_first_time
206  
-              if @counter == 0
207  
-                @counter += 1
208  
-                false
209  
-              else
210  
-                true
211  
-              end
  221
+              @counter += 1
  222
+              @counter > 0
212 223
             end
213 224
           end.new
214 225
         end
215  
-        its(:false_if_first_time) { should be_false }
  226
+        its(:false_if_first_time) { should be(false) }
216 227
       end
217 228
     end
218 229
   end

0 notes on commit a951010

Please sign in to comment.
Something went wrong with that request. Please try again.