stories
stories copied to clipboard
Validate early, error lately.
Let say we would have nested stories.
class Purchase(Story):
I.fetch_discount
when('has_discount') > I.apply_discount
In that case, we have :chicken: and :egg: problem.
- fetch discount would set
has_discountanddiscountvariables. - apply discount expect
discountto be not null domain object - if discount is null, apply discount would not be executed in any case
- if discount exists, apply discount would run
- What kind of contract we need to write for discount variable?
I could see couple of options here.
- Make discount nullable, which would broke apply discount story expectation but would not have state union error
- Raise validation error on first attribute access, not on attribute assign. We would need to have different variables for discount for different stories. Apply discount would never executed, so it would never access invalid discount variable, so validation error would not be raised.
- Do not assign invalid discount in the first place. We would have if statement inside fetch discount method which would only assign has discount variable in case discount is null.
I suggest we document pattern number 3 from the list above and call it a day.