[Rspec-users] Strategy pattern: comparing Context/Specify to Given/When/Then

aslak hellesoy aslak.hellesoy at gmail.com
Sat Aug 12 18:56:55 EDT 2006


On 8/12/06, Alvin Schur <aschur1 at telus.net> wrote:
> Comments and suggestions for improving the specifications are welcome.
>

Hi Alvin,

I don't know your domain, but I have some general comments on your use
of the GWT format.

Your GWT examples look somewhat more focused on state than behaviour.
(This might be due to my lack of domain knowledge - please let me know
if I'm wrong). I'll try to explain...

GWT is not only a format for defining acceptance criteria by example.
It is also a format that is intended to describe behaviour.

So what is behaviour? If you look it up in a dictionary
(http://webster.com/dictionary/behavior) you'll find something along
the lines of "what something does when it's poked in a certain way".

In order to describe the behaviour of something we have to give an
example of what it does in response to some stimulus. Here is an
example of how to describe a behaviour of a window when a rock is
thrown at it - using GWT:

Given a 5mm thick window
When I throw a 1kg rock at it from 3 meters away
Then it will break

Or more generally:

Given a subject in a known state
When the subject is exposed to a certain *stimulus*
Then the subject will *respond* in a certain way

The role of the When is to describe the stimulus - some sort of event.
A common mistake is to overlook this and just use the When to describe
additional state. Example:

Given I have a 5mm thick window
When the window is 1 square meter in area
Then it will be 0.005 cube meters in volume

GWT when used this way does *not* describe behaviour. It's describing
state. BDD is all about describing how things behave, and the GWT
should be used accordingly.

---

With this in mind, could your behaviour examples in the GWT format be
written differently? If that's difficult, can you think of some
stimulus or event that your system can be exposed to - and describe
that?

Cheers,
Aslak

> Thanks,
> Alvin
>
>
> One example comparing context/specify (CS) with given/when/then (GWT)
> with a strategy pattern follows:
>
> Consider a pipeline transporting oil supplied by multiple partners.
> The amounts transported on behalf of each partner are recorded as
> transactions in a general ledger.
>
> There are multiple approaches to allocating the pipeline operational
> costs to each partner:
>   - fixed amount
>   - equal split
>   - fixed percentage
>   - gross override with subsidiary cost allocation
>   - proportional cost allocation
>   - tiered cost allocation
>
> The fixed amount, equal split, and fixed percentage methods were easy
> to specify since all logic is contained within one class.  The CS and
> GWT provide similar descriptions.
>
> The remaining cost allocation methods make use of relationships
> between classes.  Then the GWT terminology seems easier to use than
> the CS terminology.
>
> Using CS and GWT certainly helped me to understand the
> inter-relationships between classes.
>
> For example:
>
> given "A cost allocation with a gross override amount of $5000 to cost
> allocation 1 and the remainder to cost allocation 2"
>   when "the allocated amount is $5000"
>   then "the amount is allocated by cost allocation 1"
>
>   when "the allocated amount is $5001"
>   then "$5000 is allocated by cost allocation 1"
>   then "$1 is allocated by cost allocation 2"
>
>
> *** Compared to ***
>
>
> context "A cost allocation with a gross override amount of $5000 to cost
> allocation 1 and the remainder based on cost allocation 2"
>   specify "should allocate an amount of $5000 based on cost allocation 1 when
> allocating $5000"
>   specify "should allocate an amount of $5000 based on cost allocation 1 when
> allocating $5001"
>   specify "should allocate an amount of $1 based on cost allocation 2 when
> allocating $5001"
>
> *** OR ***
>
> context "A cost allocation with a gross override amount of $5000 to cost
> allocation 1 and the remainder based on cost allocation 2 and allocating
> $5000"
>   specify "should allocate an amount of $5000 based on cost allocation 1"
>
> context "A cost allocation with a gross override amount of $5000 to cost
> allocation 1 and the remainder based on cost allocation 2 and allocating
> $5001"
>   specify "should allocate an amount of $5000 based on cost allocation 1"
>   specify "should allocate an amount of $1 based on cost allocation 2"
> _______________________________________________
> Rspec-users mailing list
> Rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the Rspec-users mailing list