[rspec-devel] Using Behaviours inside Examples

Bob Cotton bob.cotton at rallydev.com
Thu Jul 19 00:08:29 EDT 2007


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:

Finished in 0.134144 seconds

1 example, 1 failure



More information about the rspec-devel mailing list