This section will shortly describe the internals of BeeTest.NET. The primary object for execution in a ‘When’ fragment and validation during ‘Then’ fragments is the so called state. The state consists of zero to N (current N_max = 5) objects that were constructed during the given phase of an test. Lets see how the state changed when we use ‘Given’ Fragments to Arrange an test.

The initial state is always empty S = {}.

When

  1: this.Given();

is invoked nothing happens the state will still be S={}. This is because that given can’t change the state. Only Givens that are ‘Return-Givens’ can produce an altered state. So lets see how we can produce a new state.

  1: this.Given()
  2:     .Returns.Given(() => {return 2;});

With the additional statement the state after the second given now will be S = {int}. And now the really cool stuff of BeeTest comes into action. BeeTest takes care of passing the current state to the following behavior fragment and will also ensure, that the given fragments are compatible. If they are not compatible an compiler error will be presented.

Lets show this by an simple example.

  1:     public class SimpleCalcExample : BehaviorTestBase
  2:     {
  3:         [TestMethod]
  4:         public void SomeCalc()
  5:         {
  6:             this.Returns.Given(Ret, 2)
  7:                 .And.Returns.Given(Ret, 3)
  8:                 .Returns.When(Add)
  9:                 .Then(Ass, 5);
 10:         }
 11: 
 12:         private int Ret(int num)        { return num; }
 13:         private int Add(int a, int b)   { return a + b; }
 14:         private void Ass(int v, int e)  
 15:         { 
 16:             Assert.AreEqual(e, v);
 17: 
 18:         }
 19:     }

After the first ‘Given’ we have S = {int} and after the second 'Given’ S = {int, int}. As the ‘Returns.Given’ fragments don’t use the state themself by default there wasn’t something new or cool yet. But now look what happens when we write the ‘Returns.When’ fragment.

image

This is an screen shot of VisualStudio’s Intellisense while writing the test. As you can see, it already knows that the ‘When’ Func<…> that gets passed to it will need to handle an State S={int, int}. So it already states that the provided Func<…> you can pass to that ‘When’ needs to take two integers as the parameters. Assume the ‘Add’ method only takes one integer as the parameter. You would get a compiler error similar to the following.

  1: The type arguments for method 'BeeTest.ReturnWhenState<int,int>.When<TR>(System.Func<int,int,TR>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
  2: 

The ‘Return.When’ also alters the state to S = {int}. So instead of expanding the state like a ‘Returns.Given’ does the ‘Returns.When’ fragment completely replaces the state build up by the ‘Returns.Given’. Although the Ass has two integer parameters only the first one will automatically be bound to the runtime value of the state. The second one is supplied via our code as an test parameter (the expected value).

Last edited Apr 14, 2013 at 2:02 PM by paiden36, version 2

Comments

No comments yet.