[rspec-users] shared behav

Rick DeNatale rick.denatale at gmail.com
Tue Dec 18 17:38:09 EST 2007


On 12/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:

> You'd use it like this:
>
> shared_examples_for "anything" do
>   it "should do stuff" do
>     @thing.should do_stuff
>   end
> end
>
> describe SpecificThing do
>   it_should_behave_like "anything", :thing => SpecificThing.new
> end
>
> Now comes the tricky part. This can work one of two ways. In either
> case it turns :thing into an instance variable @thing. The question is
> whether that instance variable is assigned in the scope of the
> SpecificThing example group or in a scope created especially to run
> the shared examples. Each choice presents usability confusion in my
> view.
>
> Imagine this scenario:
>
> shared_examples_for "anything" do
>   it "should do stuff" do
>     @thing.should do_stuff
>   end
>   it "should be like another thing" do
>     @thing.should be_like(@other_thing)
>   end
> end
>
> describe SpecificThing do
>   before(:each) do
>     @thing = SpecificThing.new
>   end
>   it_should_behave_like "anything", :other_thing => SpecificThing.new
>
>   it "should not be unlike other thing" do
>     @thing.should_not be_unlike(@other_thing)
>   end
> end
>
> If @other_thing is defined in the context of a separate scope just to
> run the shared examples, the shared examples won't know about @thing.
>
> If @other_thing in the scope of the example group created by "describe
> SpecificThing," then it's just confusing to look at - both
> before(:each) and it_should_behave_like are creating instance
> variables in the current scope, and it is NOT clear that is what is
> happening.
>
> If we go w/ the separate scope, then the metaphor of
> it_should_behave_like is wrong, because we're not talking about
> something in the current scope that should behave like something else
> anymore. We'd probably want a different construct to create these,
> even though the underlying mechanism would be the same.
>
> Thoughts?

How about something like (following the example of things like the
:include option in ActiveRecord::Base#find:

  it_should_behave_like "anything", :except_that => { :thing =>
SpecificThing.new}

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/


More information about the rspec-users mailing list