Permalink
Browse files

Added spec and yard docs for #subject! method, and improved cuke exam…

…ple.
  • Loading branch information...
1 parent 027dcd4 commit d6018ac8088402b1a800ae00e76300f229dfa603 @zubin zubin committed Dec 30, 2012
Showing with 72 additions and 5 deletions.
  1. +9 −5 features/subject/explicit_subject.feature
  2. +50 −0 lib/rspec/core/subject.rb
  3. +13 −0 spec/rspec/core/subject_spec.rb
@@ -85,11 +85,15 @@ Feature: explicit subject
Given a file named "subject_bang_spec.rb" with:
"""ruby
describe Array do
- subject! { prepared_array.push(1,2,3) }
- let(:prepared_array) { [] }
- it "evaluates subject first" do
- prepared_array.push(4,5,6)
- prepared_array.should == [1,2,3,4,5,6]
+ describe '#pop' do
+ let(:prepared_array) { [1,2,3] }
+ subject! { prepared_array.pop }
+ it "removes the last value from the array" do
+ prepared_array.should eq([1,2])
+ end
+ it "returns the last value of the array" do
+ subject.should eq(3)
+ end
end
end
"""
View
@@ -204,6 +204,56 @@ def subject(name=nil, &block)
# Just like `subject`, except the block is invoked by an implicit `before`
# hook. This serves a dual purpose of setting up state and providing a
# memoized reference to that state.
+ #
+ # @example
+ #
+ # class Thing
+ # def self.count
+ # @count ||= 0
+ # end
+ #
+ # def self.count=(val)
+ # @count += val
+ # end
+ #
+ # def self.reset_count
+ # @count = 0
+ # end
+ #
+ # def initialize
+ # self.class.count += 1
+ # end
+ # end
+ #
+ # describe Thing do
+ # after(:each) { Thing.reset_count }
+ #
+ # context "using subject" do
+ # subject { Thing.new }
+ #
+ # it "is not invoked implicitly" do
+ # Thing.count.should eq(0)
+ # end
+ #
+ # it "can be invoked explicitly" do
+ # subject
+ # Thing.count.should eq(1)
+ # end
+ # end
+ #
+ # context "using subject!" do
+ # subject!(:thing) { Thing.new }
+ #
+ # it "is invoked implicitly" do
+ # Thing.count.should eq(1)
+ # end
+ #
+ # it "returns memoized version on first invocation" do
+ # subject
+ # Thing.count.should eq(1)
+ # end
+ # end
+ # end
def subject!(name=nil, &block)
subject(name, &block)
before { __send__(:subject) }
@@ -251,5 +251,18 @@ def false_if_first_time
its(:false_if_first_time) { should be(false) }
end
end
+
+ describe '#subject!' do
+ let(:prepared_array) { [1,2,3] }
+ subject! { prepared_array.pop }
+
+ it "evaluates subject before example" do
+ prepared_array.should eq([1,2])
+ end
+
+ it "returns memoized value from first invocation" do
+ subject.should eq(3)
+ end
+ end
end
end

0 comments on commit d6018ac

Please sign in to comment.