I have seen what it seems to me are two approaches to BDD. The difference hinges on the location of the "when":
In approach 1 the when is part of the specification:
AnEmptyStack.isNoLongerEmptyAfterPush
In pure "given when then" terminology this is:
"Given an empty stack, when it is pushed, then it is no longer empty."
So the "when" is part of the specification method:
isNoLongerEmptyAfterPush(){ stack.push(anObject); Assert.notEmpty(stack); }
In approach 2 the when is defined at the class level. That is, the when is usually invoked in the setup.
class WhenAnEmptyStackIsPushed(){
setup(){ stack.push(); }
public void thenItIsNotEmpty(){ assert(stack.notEmpty()) } }
Is there a preferred method? In terms of pure testing of behaviour, the second option seems preferable to me, since the focus of the test fixture is on the behaviour.
However, for ease of testing, I'm leaning towards the first method. Most of the pain I find in testing is the setup. That is, I have to get a SUT in a particular state. Once in that state usually its only one line of code to actually invoke some behaviour on it. So, having multiple behaviours per class (that is, per setup context) leverages the one time setup of the class.
So, I'm looking for thoughts. Is one approach preferred over the other?