Skip to content
MattesGroeger edited this page Mar 18, 2012 · 15 revisions

Specs | Expectations | Asynchronous Testing | Mocks and Stubs

Kiwi specs have the following elements

  • #import "Kiwi.h" brings in the Kiwi library. This should be the last header imported in the spec.
  • SPEC_BEGIN(ClassName) and SPEC_END are macros that expand to begin and end a KWSpec class and example group declaration.
  • registerMatchers(aNamespacePrefix) registers all matchers that begin with the specified namespace prefix. Other than the default Kiwi matchers, these are the matchers that will be available for use in the spec.
  • describe(aString, aBlock) starts a context that can contain examples and nested contexts.
  • context(aString, aBlock) is a synonym for describe.
  • To be make a block variable mutable, it needs to have the __block qualifier.
  • beforeAll(aBlock) is run once before all the inner contexts and it blocks of the context it is in.
  • afterAll(aBlock) is run once after all the inner contexts and it block of the context it is in.
  • beforeEach(aBlock) is run before every it block in all enclosed contexts. Code that actually sets up the particular context should go here.
  • afterEach(aBlock) is run after every it block in all enclosed contexts.
  • it(aString, aBlock) declares an example. This is where actual expectations on objects should go.
  • pending(aString, aBlock) doesn't do anything other than log a pending message to the output when run.

Example

#import "Kiwi.h"

SPEC_BEGIN(SpecName)

describe(@"ClassName", ^{
    registerMatchers("BG"); // Registers BGTangentMatcher, BGConvexMatcher, etc.
    
    context(@"a state the component is in", ^{
        __block id variable = nil;

        beforeAll(^{ // Occurs once
        });

        afterAll(^{ // Occurs once
        });

        beforeEach(^{ // Occurs before each enclosed "it"
            variable = [MyClass instance];
        });

        afterEach(^{ // Occurs after each enclosed "it"
        });

        it(@"should do something", ^{
            [[variable should] meetSomeExpectation];
        });

        context(@"inner context", ^{
            it(@"does another thing", ^{
            })

            pending(@"something unimplemented", ^{
            });
        })
    });
});

SPEC_END

Stubs in Examples

All stubs are always cleared at the end of a spec example (an it block).