[rspec-users] first cut at blockless given/when/then

David Chelimsky dchelimsky at gmail.com
Thu Oct 18 10:30:45 EDT 2007


On 10/18/07, Pat Maddox <pergesu at gmail.com> wrote:
> On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote:
> > > El 18/10/2007, a las 10:33, "David Chelimsky" <dchelimsky at gmail.com>
> > > escribió:
> > >
> > > > Take a look at examples/story/calculator.rb to see what's going on.
> > > >
> > > > Needs docs!!!!
> > > >
> > > > Thoughts welcome.
> > >
> > > Could this:
> > >
> > >    step_matcher(:given, "an addend of $addend") do |addend|
> > >      @adder ||= Adder.new
> > >      @adder << addend.to_i
> > >    end
> > >
> > >    step_matcher(:when, "they are added") do
> > >      @sum = @adder.sum
> > >    end
> > >
> > >    step_matcher(:then, "the sum should be $sum") do |sum|
> > >      @sum.should == sum.to_i
> > >    end
> > >
> > > Be refactored to this?
> > >
> > >    given_matcher("an addend of $addend") do |addend|
> > >      @adder ||= Adder.new
> > >      @adder << addend.to_i
> > >    end
> > >
> > >    when_matcher("they are added") do
> > >      @sum = @adder.sum
> > >    end
> > >
> > >    then_matcher("the sum should be $sum") do |sum|
> > >      @sum.should == sum.to_i
> > >    end
> > >
> > > I'd find that a little bit easier to type; what do you think?
> >
> > Easier to type, sure. I'm not in love w/ the names yet though because
> > they sound like verb phrases - "given matcher", "when matcher", "then
> > matcher".
> >
> > How about something like match_given, match_when, match_then?
>
> I see what you're saying.  However I think it's helpful to put the
> g/w/t right at the beginning of the method name, it's easier to
> distinguish between them.  You can look at the first character and
> know what's going on.  With step_matcher(:given...) you have to skip
> the first 12 characters.  That feels noisy and dirty to me, and I
> think the readability overcomes the verb phrase problem.
>
> Though I agree with you and I'm not in love with given_matcher.  hrm,
> needs more thought.

What if it were wrapped in something like this:

step_matchers do
  given_matcher("an addend of $addend") do |addend|
    @adder ||= Adder.new
    @adder << addend.to_i
  end

  when_matcher("they are added") do
    @sum = @adder.sum
  end

  then_matcher("the sum should be $sum") do |sum|
    @sum.should == sum.to_i
  end
end

step_matchers provides context for the [given|when|then]_matcher methods. WDYT?


More information about the rspec-users mailing list