My favorites | Sign in
Project Logo
                
Search
for
Updated May 19, 2008 by goo...@tiestvilee.fastmail.com.au
SetupNesting  
Use the 'When' function to nest setups

Setup Nesting

To reduce the amount of setup required in individual tests, but also make the setup code local to particular tests, you can use the 'when' clause to nest your set up.

var mockObject = {
  returnSomething = function() {}
};

testCases(test,
  function setUp() {
    mockObject.returnSomething = function() {return 1};
  },
  function basicSetUp() {
    assert.that(mockObject.returnSomething(), eq(1));
  },
  when(
    function returnSomethingReturnsHello() {
      var whenLevel1 = mockObject.returnSomething();
      mockObject.returnSomething = function() {return "hello" + whenLevel1;};
    },
    function insideFirstWhen() {
      assert.that(mockObject.returnSomething(), eq("hello1"));
    },
    when(
      function returnSomethingReturnsObject() {
        var whenLevel2 = mockObject.returnSomething();
        mockObject.returnSomething = function() {return {prop : whenLevel2}};
      },
      function insideSecondWhen() {
        assert.that(mockObject.returnSomething().prop, eq("hello1"));
      }
    )
  ),
  function testStillUsingOnlyFirstSetup() {
    assert.that(mockObject.returnSomething(), eq(1));
  }
);

In the above code snipet we have three levels of nesting; the original SetUp, and then two nested 'when's.


Comment by miguel.villarreal, Jul 08, 2009

WHAT IS THIS FOR CAN YOU SEND E A VIDEO OF WHAT THIS IS AND HOW I CAN USE IT OK miguel.villarreal@gmail.com


Sign in to add a comment
Hosted by Google Code