Skip to content
Dan Trenz edited this page May 30, 2015 · 15 revisions

Specs | Expectations | Mocks and Stubs | Asynchronous Testing

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 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.
  • specify(aBlock) declares an example without description. This can be used for straightforward expectations.
  • pending(aString, aBlock) doesn't do anything other than log a pending message to the output when run.
  • let(subject, aBlock) declares a local helper variable that is re-initialized before every it block in all enclosed contexts.

Example

    #import "Kiwi.h"
    
    SPEC_BEGIN(SpecName)
    
    describe(@"ClassName", ^{
        registerMatchers(@"BG"); // Registers BGTangentMatcher, BGConvexMatcher, etc.
        
        context(@"a state the component is in", ^{
            let(variable, ^{ // Occurs before each enclosed "it"
                return [MyClass instance];
            });
    
            beforeAll(^{ // Occurs once
            });
    
            afterAll(^{ // Occurs once
            });
    
            beforeEach(^{ // Occurs before each enclosed "it"
            });

            afterEach(^{ // Occurs after each enclosed "it"
            });
    
            it(@"should do something", ^{
                [[variable should] meetSomeExpectation];
            });

            specify(^{
                [[variable shouldNot] beNil];
            });
    
            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).