[rspec-devel] Using Behaviours inside Examples

Bob Cotton bob.cotton at rallydev.com
Thu Jul 19 14:45:03 EDT 2007


"David Chelimsky" <dchelimsky at gmail.com> writes:

> On 7/19/07, Bob Cotton <bob.cotton at rallydev.com> wrote:
>> "David Chelimsky" <dchelimsky at gmail.com> writes:
>>
>> > 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.
>>
>> Cool, thanks.
>>
>> One more question, if I'm using mocks in the "outer" Example, will
>> calling Behaviour#run reset them?
>
> I don't understand your question. What is the "outer" example?

Sorry about being so cryptic.

>> I'm seeing it in my spec, but have not been able to create an isolated
>> spec.

I was able to isolate it. It seems that Reporter#dump is clearing the
mock.

class CustomFormatter < Spec::Runner::Formatter::BaseFormatter
  def initialize(where, m)  # <-  the mock is new from the last example
    super(where)
    @mock = m
  end
  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"
    @mock.call_method("hello")
  end
end

describe "Mixed Behaviours" do
  before(:each) do
    @m = mock("mock")
    @m.should_receive(:call_method).with("hello")

    @formatter = CustomFormatter.new(nil, @m)
    @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") do
      it("should") {true.should == true}
    end

    behaviour.run(@reporter)
    @reporter.dump

    @formatter.last_method.should == "dump_summary"
  end
end

> spec reporter_spec.rb
F

1)
Spec::Mocks::MockExpectationError in 'Mixed Behaviours should allow use of Behaviours in examples'
Mock 'mock' received unexpected message :call_method with ("hello")
./reporter_spec.rb:15:in `dump_summary'
./reporter_spec.rb:34:

Finished in 0.056315 seconds

1 example, 1 failure


More information about the rspec-devel mailing list