allending/Kiwi — Issue #103: Add before/afterEachContext
Author: gcox
Created: 2012-03-07 12:59:17+00:00
State: open

This is an example of how I'm formatting my tests now:

describe(@"When doing something and", ^{
context(@"it fails because of reason 1", ^{
// setup
it("it should do thing 1", ^{
});
it("it should do thing 2", ^{
});
});
context(@"it fails because of reason 2", ^{
// setup
it("it should do thing 3", ^{
});
it("it should do thing 4", ^{
});
});
context(@"it succeeds", ^{
// setup
it("it should do thing 5", ^{
});
it("it should do thing 6", ^{
});
});
});

In most of my tests, much of the "// setup" bit gets repeated (or I create my own 'setupContext' block that I call before the "it"s. Then I'll have one or two lines actually specific to the given context. Not perfect obviously

It would be nice if the above could be rewritten as:

describe(@"When doing something and", ^{

beforeEachContext(^{
do something;
});
afterEachContext(^{
clean something up;
});

context(@"it fails because of reason 1", ^{
it("it should do thing 1", ^{
});
it("it should do thing 2", ^{
});
});
context(@"it fails because of reason 2", ^{
it("it should do thing 3", ^{
});
it("it should do thing 4", ^{
});
});
context(@"it succeeds", ^{
it("it should do thing 5", ^{
});
it("it should do thing 6", ^{
});
});
});

Maybe there's a way to pull this off now, I'm just not seeing it.