1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
5 describe Example, " instance" do
6 # TODO - this should be
7 # predicate_matchers :is_a
13 @reporter = stub("reporter", :example_started => nil, :example_finished => nil)
16 it "should send reporter example_started" do
17 example=Example.new("example") {}
18 @reporter.should_receive(:example_started).with("example")
19 example.run(@reporter, nil, nil, false, nil)
22 it "should report its name for dry run" do
23 example=Example.new("example") {}
24 @reporter.should_receive(:example_finished).with("example")
25 example.run(@reporter, nil, nil, true, nil) #4th arg indicates dry run
28 it "should report success" do
29 example=Example.new("example") {}
30 @reporter.should_receive(:example_finished).with("example", nil, nil, false)
31 example.run(@reporter, nil, nil, nil, nil)
34 it "should report failure due to failure" do
35 example=Example.new("example") do
38 @reporter.should_receive(:example_finished).with("example", is_a(Spec::Expectations::ExpectationNotMetError), "example", false)
39 example.run(@reporter, nil, nil, nil, nil)
42 it "should report failure due to error" do
43 error=RuntimeError.new
44 example=Example.new("example") do
47 @reporter.should_receive(:example_finished).with("example", error, "example", false)
48 example.run(@reporter, nil, nil, nil, nil)
51 it "should run example in scope of supplied object" do
52 scope_class = Class.new
53 example=Example.new("should pass") do
54 self.instance_of?(Example).should == false
55 self.instance_of?(scope_class).should == true
57 @reporter.should_receive(:example_finished).with("should pass", nil, nil, false)
58 example.run(@reporter, nil, nil, nil, scope_class.new)
61 it "should not run example block if before_each fails" do
63 example=Example.new("should pass") {example_ran = true}
64 before_each = lambda {raise "Setup error"}
65 example.run(@reporter, before_each, nil, nil, Object.new)
66 example_ran.should == false
69 it "should run after_each block if before_each fails" do
70 after_each_ran = false
71 example=Example.new("should pass") {}
72 before_each = lambda {raise "Setup error"}
73 after_each = lambda {after_each_ran = true}
74 example.run(@reporter, before_each, after_each, nil, Object.new)
75 after_each_ran.should == true
78 it "should run after_each block when example fails" do
79 example=Example.new("example") do
83 raise("in after_each")
85 @reporter.should_receive(:example_finished) do |example, error, location|
86 example.should eql("example")
87 location.should eql("example")
88 error.message.should eql("in body")
90 example.run(@reporter, nil, after_each, nil, nil)
93 it "should report failure location when in before_each" do
94 example=Example.new("example") {}
95 before_each=lambda { raise("in before_each") }
96 @reporter.should_receive(:example_finished) do |name, error, location|
97 name.should eql("example")
98 error.message.should eql("in before_each")
99 location.should eql("before(:each)")
101 example.run(@reporter, before_each, nil, nil, nil)
104 it "should report failure location when in after_each" do
105 example = Example.new("example") {}
106 after_each = lambda { raise("in after_each") }
107 @reporter.should_receive(:example_finished) do |name, error, location|
108 name.should eql("example")
109 error.message.should eql("in after_each")
110 location.should eql("after(:each)")
112 example.run(@reporter, nil, after_each, nil, nil)
115 it "should accept an options hash following the example name" do
116 example = Example.new("name", :key => 'value')
119 it "should report NO NAME when told to use generated description with --dry-run" do
120 example = Example.new(:__generate_description) {
123 @reporter.should_receive(:example_finished) do |desc, error, location|
124 desc.should == "NO NAME (Because of --dry-run)"
126 example.run(@reporter, lambda{}, lambda{}, true, Object.new)
129 it "should report NO NAME when told to use generated description with no expectations" do
130 example = Example.new(:__generate_description) {}
131 @reporter.should_receive(:example_finished) do |desc, error, location|
132 desc.should == "NO NAME (Because there were no expectations)"
134 example.run(@reporter, lambda{}, lambda{}, false, Object.new)
137 it "should report NO NAME when told to use generated description and matcher fails" do
138 example = Example.new(:__generate_description) do
139 5.should "" # Has no matches? method..
141 @reporter.should_receive(:example_finished) do |desc, error, location|
142 desc.should == "NO NAME (Because of Error raised in matcher)"
144 example.run(@reporter, nil, nil, nil, Object.new)
147 it "should report generated description when told to and it is available" do
148 example = Example.new(:__generate_description) {
151 @reporter.should_receive(:example_finished).with("should == 5", anything(), anything(), false)
152 example.run(@reporter, nil, nil, nil, Object.new)
155 it "should unregister description_generated callback (lest a memory leak should build up)" do
156 example = Example.new("something")
157 Spec::Matchers.should_receive(:unregister_description_generated).with(is_a(Proc))
158 example.run(@reporter, nil, nil, nil, Object.new)