[rspec-devel] Using Behaviours inside Examples

David Chelimsky dchelimsky at gmail.com
Thu Jul 19 00:29:05 EDT 2007


On 7/19/07, Bob Cotton <bob.cotton at rallydev.com> wrote:
>
> I'm specing this custom formatter that relies on the entire life-cycle
> of the formatter class, and will perform the interesting work during
> dump_summary.
>
> In my spec I didn't want to replicate all the calls that the formatter
> receives, but instead cook up a Behaviour, then run that with my
> formatter.
>
> My question is: should I be able to do this? The following spec says
> not, but i'm not sure why.
>
> class CustomFormatter < Spec::Runner::Formatter::BaseFormatter
>   attr_reader :last_method
>   def add_behaviour(name)
>     @last_method = "add_behaviour"
>   end
>
>   def dump_summary(duration, example_count, failure_count, pending_count)
>     @last_method = "dump_summary"
>   end
> end
>
> describe "Mixed Behaviours" do
>   before(:each) do
>     @formatter = CustomFormatter.new(nil)
>     @reporter = Spec::Runner::Reporter.new([@formatter], mock("backtrace_tweaker", :null_object => true))
>   end
>
>   it "should allow use of Behaviours in examples" do
>     behaviour = Spec::DSL::Behaviour.new("My Behaviour") {}
>     behaviour.it("should") {true.should == true}
>
>     behaviour.run(@reporter)
>     @formatter.last_method.should == "dump_summary"
>   end
> end
>
> > spec reporter_spec.rb
> F
>
> 1)
> 'Mixed Behaviours should allow use of Behaviours in examples' FAILED
> expected: "dump_summary",
>      got: "add_behaviour" (using ==)
> ./reporter_spec.rb:25:

dump_summary is never getting called when you run a single behaviour
(i.e. tell that behaviour to run). dump_summary is called from the
BehaviourRunner.

What you can do is call @reporter.dump and that would do it.

>
> Finished in 0.134144 seconds
>
> 1 example, 1 failure
>
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the rspec-devel mailing list