Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
Fixed bug where subject (implicit and explicit) were not being exposed
Browse files Browse the repository at this point in the history
[#744 state:resolved milestone:'Next Release']
  • Loading branch information
dchelimsky committed Mar 20, 2009
1 parent 24cd8f5 commit ed27d45
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 34 deletions.
31 changes: 31 additions & 0 deletions features/subject/explicit_subject.feature
@@ -0,0 +1,31 @@
Feature: explicit subject

You can override the implicit subject using the subject() method.

Scenario: subject in top level group
Given the following spec:
"""
describe Array, "with some elements" do
subject { [1,2,3] }
it "should have the prescribed elements" do
subject.should == [1,2,3]
end
end
"""
When I run it with the spec command
Then the stdout should match "1 example, 0 failures"

Scenario: subject in a nested group
Given the following spec:
"""
describe Array do
subject { [1,2,3] }
describe "with some elements" do
it "should have the prescribed elements" do
subject.should == [1,2,3]
end
end
end
"""
When I run it with the spec command
Then the stdout should match "1 example, 0 failures"
31 changes: 31 additions & 0 deletions features/subject/implicit_subject.feature
@@ -0,0 +1,31 @@
Feature: implicit subject

The first argument to the outermost example group block is
made available to each example as an implicit subject of
that example.

Scenario: subject in top level group
Given the following spec:
"""
describe Array, "when first created" do
it "should be empty" do
subject.should == []
end
end
"""
When I run it with the spec command
Then the stdout should match "1 example, 0 failures"

Scenario: subject in a nested group
Given the following spec:
"""
describe Array do
describe "when first created" do
it "should be empty" do
subject.should == []
end
end
end
"""
When I run it with the spec command
Then the stdout should match "1 example, 0 failures"
19 changes: 15 additions & 4 deletions lib/spec/example/subject.rb
Expand Up @@ -15,13 +15,24 @@ module ExampleGroupMethods
#
# See +ExampleMethods#should+ for more information about this approach.
def subject(&block)
if block.nil?
defined?(@_subject_block) ? @_subject_block :
(described_class ? lambda {described_class.new} : lambda {description_args.first})
block.nil? ?
explicit_subject || implicit_subject :
@_explicit_subject_block = block
end

def explicit_subject
if defined?(@_explicit_subject_block)
@_explicit_subject_block
elsif super_subject = superclass.instance_variable_get('@_explicit_subject_block')
super_subject
else
@_subject_block = block
nil
end
end

def implicit_subject
(described_class ? lambda {described_class.new} : lambda {description_args.first})
end
end

module ExampleMethods
Expand Down
30 changes: 0 additions & 30 deletions spec/spec/example/example_methods_spec.rb
Expand Up @@ -56,36 +56,6 @@ def module_that_is_reopened_method; end
end
end

describe "#subject" do
before(:each) do
@example_group = ExampleGroupDouble
end

it "should return an instance of the described class" do
group = Class.new(ExampleGroupDouble).describe(Array)
example = group.new(ExampleProxy.new)
example.subject.should == []
end

it "should return a Module" do
group = Class.new(ExampleGroupDouble).describe(Enumerable)
example = group.new(ExampleProxy.new)
example.subject.should == Enumerable
end

it "should return a string" do
group = Class.new(ExampleGroupDouble).describe('foo')
example = group.new(ExampleProxy.new)
example.subject.should == 'foo'
end

it "should return a number" do
group = Class.new(ExampleGroupDouble).describe(15)
example = group.new(ExampleProxy.new)
example.subject.should == 15
end
end

describe "#should" do
before(:each) do
@example_group = Class.new(ExampleGroupDouble)
Expand Down
77 changes: 77 additions & 0 deletions spec/spec/example/subject_spec.rb
@@ -0,0 +1,77 @@
require File.dirname(__FILE__) + '/../../spec_helper'

module Spec
module Example
describe "implicit subject" do
describe "with a class" do
it "returns an instance of the class" do
group = Class.new(ExampleGroupDouble).describe(Array)
example = group.new(ExampleProxy.new)
example.subject.should == []
end
end

describe "with a Module" do
it "returns the Module" do
group = Class.new(ExampleGroupDouble).describe(Enumerable)
example = group.new(ExampleProxy.new)
example.subject.should == Enumerable
end
end

describe "with a string" do
it "return the string" do
group = Class.new(ExampleGroupDouble).describe('foo')
example = group.new(ExampleProxy.new)
example.subject.should == 'foo'
end
end

describe "with a number" do
it "returns the number" do
group = Class.new(ExampleGroupDouble).describe(15)
example = group.new(ExampleProxy.new)
example.subject.should == 15
end
end

end

describe "explicit subject" do
describe "defined in a top level group" do
it "replaces the implicit subject in that group" do
group = Class.new(ExampleGroupDouble).describe(Array)
group.subject { [1,2,3] }
example = group.new(ExampleProxy.new)
example.subject.should == [1,2,3]
end
end

describe "defined in a top level group" do
it "is available in a nested group (subclass)" do
klass = Class.new do
extend Spec::Example::Subject::ExampleGroupMethods
include Spec::Example::Subject::ExampleMethods
class << self
def described_class
Array
end
end
def described_class
self.class.described_class
end

subject {
[1,2,3]
}
end

subclass = Class.new(klass)

object = subclass.new
object.subject.should == [1,2,3]
end
end
end
end
end

0 comments on commit ed27d45

Please sign in to comment.