The first step to set up a Behavior Test is to arrange the test. The arrangement is done with the ‘Given’ and ‘Returns.Given’ fragments from the BehaviorTestBase class. Lets assume we have an class called Foo that shall be tested and that looks something like this.

  1:         public class Foo
  2:         {
  3:             public Foo parent;
  4: 
  5:             public static void StaticFooInit() { }
  6:             public Foo();
  7:             public Foo(Foo p) { this.parent = p;}
  8:             public void SomeOperation()  { }
  9:             public void InitSomeMember(int i) { }
 10:         }

Additionally the test class defines the following behavior fragments:

  1:         private Foo FooDefaultConstructed()
  2:         {
  3:             return new Foo();
  4:         }
  5: 
  6:         private Foo FooWithParentConstructed(Foo f)
  7:         {
  8:             return new Foo(f);
  9:         }
 10: 
 11:         private void FooMemberWasInitializedWith(Foo f, int value)
 12:         {
 13:             f.InitSomeMember(value);
 14:         }

Then a simple arrangement for a test case could look something like this.

 

  1:         [TestMethod]
  2:         public void SomeTest()
  3:         {
  4:             this.Given(Foo.StaticFooInit)
  5:                 .And.Returns.Given(FooDefaultConstructed)
  6:                 .And.Given(FooMemberWasInitializedWith, 666);
  7:         }

Not a ‘Given’ fragment always gets the state constructed by the previous givens as ‘Given’ calls normally do additional operations on already given objects. This is not the case with ‘Returns.Given’ fragments, as their purpose normally is to create new objects. But the behavior ‘FooWithParentConstructed’ needs a fool that was already constructed. So how can we get BeeTest.Net to automatically forward an already constructed object to another ‘Returns.Given’ call. This is done with the Forward class as shown in the following example.

  1:         private void InitializeFoos(Foo f1, Foo f2, Foo f3, int f1Value, int f2Value, int f3Value)
  2:         {
  3:             FooMemberWasInitializedWith(f1, f1Value);
  4:             FooMemberWasInitializedWith(f2, f2Value);
  5:             FooMemberWasInitializedWith(f3, f3Value);
  6:         }
  7: 
  8:         [TestMethod]
  9:         public void SomeTest()
 10:         {
 11:             this.Given(Foo.StaticFooInit)
 12:                 .And.Returns.Given(FooDefaultConstructed)
 13:                 .And.Returns.Given(FooWithParentConstructed, Forward<Foo>.Given0)
 14:                 .And.Returns.Given(FooWithParentConstructed, Forward<Foo, Foo>.Given1)
 15:                 .And.Given(InitializeFoos, 666, 777, 888);
 16:         }

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

Comments

No comments yet.