[Feature request] Pass scenario state as a parameter instead of using a mutable variable

Aug 27, 2013 at 11:52 AM
Edited Aug 27, 2013 at 11:54 AM

Thank you for this great library. I tried it a bit and I think that it would be great if Tickspec used a more "functional" approach to the function call. At the moment, it is necessary to keep a mutable object the keep the state of the currently running scenario. But it would be much better if the state is just passed along as the first parameter of the step definition.

This will allow TickSpec to be threadsafe and speed up the test execution (in NCrunch for example)

Example, it would give something like this:
Scenario 1: Refunded items should be returned to stock
  Given a customer buys a black jumper
  And I have 3 black jumpers left in stock 
  When he returns the jumper for a refund 
  Then I should have 4 black jumpers in stock 

// instead of a mutable variable:
// let mutable stockItem = { Count = 0 }

// pass the state object as the first parameter of each step
let [<Given>] ``a customer buys a black jumper`` (state:StockItem) =
    { Count = 0 }
let [<Given>] ``I have (.*) black jumpers left in stock`` (state:StockItem) (n:int) =  
    { state with Count = n }
let [<When>] ``he returns the jumper for a refund`` (state:StockItem) () =  
    {  state with Count = state.Count + 1 }
let [<Then>] ``I should have (.*) black jumpers in stock`` (state:StockItem) (n:int) =     
    let passed = (state.Count = n)
Aug 27, 2013 at 5:59 PM
I really like this idea, I'll take a look at what it would take to implement it.

In the meantime, there is a potential functional approach you could take which eschews reflection see: http://trelford.com/blog/post/FunkyBDD.aspx