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

Alvin Schur aschur1 at telus.net
Sat Aug 12 17:18:21 EDT 2006


Comments and suggestions for improving the specifications are welcome.

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"


More information about the Rspec-users mailing list