Skip to content

Commit

Permalink
A proper test of the precondition method
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Smith committed Mar 26, 2012
1 parent f051d99 commit 62096ea
Showing 1 changed file with 44 additions and 53 deletions.
97 changes: 44 additions & 53 deletions Specs/OCDSpecDescriptionSpec.m
Expand Up @@ -4,32 +4,37 @@
#import "OCDSpec/OCDSpecDescription.h"

CONTEXT(OCDSpecDescription){
__block OCDSpecDescription *description;
__block OCDSpecOutputter *outputter;

describe(@"The Description",
it(@"describes one example without errors", ^{
OCDSpecDescription *description = [[OCDSpecDescription alloc] init] ;

[description describe:@"It Should Do Something" onArrayOfExamples:[[NSArray alloc] init] ];
beforeEach(^{
description = [[OCDSpecDescription alloc] init];
outputter = [OCDSpecOutputter sharedOutputter];
outputter.fileHandle = [NSFileHandle fileHandleWithNullDevice];
}),

afterEach(^{
outputter.fileHandle = [NSFileHandle fileHandleWithStandardError];
}),

it(@"describes one example without errors", ^{
[description describe:@"It Should Do Something" onArrayOfExamples:[[NSArray alloc] init]];

[expect(description.failures) toBeEqualTo:[NSNumber numberWithInt:0]];
}),

it(@"describes an example with one error", ^{
OCDSpecDescription *description = [[OCDSpecDescription alloc] init];
OCDSpecOutputter *outputter = [OCDSpecOutputter sharedOutputter];
outputter.fileHandle = [NSFileHandle fileHandleWithNullDevice];

MockExample *example = [MockExample exampleThatFailed];
NSArray *examples = [NSArray arrayWithObjects:example, nil];

[description describe:@"It Should Do Something" onArrayOfExamples:examples];

outputter.fileHandle = [NSFileHandle fileHandleWithStandardError];

[expect(description.failures) toBeEqualTo:[NSNumber numberWithInt:1]];
}),

it(@"writes the exceptions to the shared outputter", ^{
OCDSpecDescription *description = [[OCDSpecDescription alloc] init];
__block NSString *outputException;
[OCDSpecOutputter withRedirectedOutput:^{
OCDSpecExample *example = [[OCDSpecExample alloc] initWithBlock:^{
Expand All @@ -49,10 +54,6 @@
}),

it(@"can describe multiple examples", ^{
OCDSpecDescription *description = [[OCDSpecDescription alloc] init];
OCDSpecOutputter *outputter = [OCDSpecOutputter sharedOutputter];
outputter.fileHandle = [NSFileHandle fileHandleWithNullDevice];

OCDSpecExample *exampleOne = [[OCDSpecExample alloc] initWithBlock:^{
FAIL(@"Fail One");
}];
Expand All @@ -64,16 +65,10 @@

[description describe:@"It Should Do Something" onArrayOfExamples:tests];

outputter.fileHandle = [NSFileHandle fileHandleWithStandardError];

[expect(description.failures) toBeEqualTo:[NSNumber numberWithInt:2]];
}),

it(@"can describe multiple successes", ^{
OCDSpecDescription *description = [[OCDSpecDescription alloc] init];
OCDSpecOutputter *outputter = [OCDSpecOutputter sharedOutputter];
outputter.fileHandle = [NSFileHandle fileHandleWithNullDevice];

OCDSpecExample *exampleOne = [[OCDSpecExample alloc] initWithBlock:^{
}];
OCDSpecExample *exampleTwo = [[OCDSpecExample alloc] initWithBlock:^{
Expand All @@ -83,18 +78,40 @@

[description describe:@"It Should Do Something" onArrayOfExamples:tests];

outputter.fileHandle = [NSFileHandle fileHandleWithStandardError];

[expect(description.successes) toBeEqualTo:[NSNumber numberWithInt:2]];
}),

it(@"runs a precondition before each example", ^{
__block NSMutableArray *callsMade = [[NSMutableArray alloc] init];

OCDSpecExample *exampleOne = [[OCDSpecExample alloc] initWithBlock:^{
[callsMade addObject:@"Ran First Example"];
}];
OCDSpecExample *exampleTwo = [[OCDSpecExample alloc] initWithBlock:^{
[callsMade addObject:@"Ran Second Example"];
}];

NSArray *tests = [NSArray arrayWithObjects:exampleOne, exampleTwo, nil];

OCDSpecDescription *description = [[OCDSpecDescription alloc] initWithName:@"Something" examples:tests];
description.precondition = ^{
[callsMade addObject:@"Precondition Called"];
};
[description describe];

[expect(callsMade) toBeEqualTo:[NSArray arrayWithObjects:@"Precondition Called",
@"Ran First Example",
@"Precondition Called",
@"Ran Second Example",
nil ]];
}),

it(@"Will still run the postcondition even if the example throws an exception", ^{
__block bool calledPost = NO;

OCDSpecExample *exampleGoBoom = [[OCDSpecExample alloc] initWithBlock:
^{
[NSException raise:@"OH NO" format:@"This is a test exception"];
}];
OCDSpecExample *exampleGoBoom = [[OCDSpecExample alloc] initWithBlock:^{
[NSException raise:@"OH NO" format:@"This is a test exception"];
}];

NSArray *tests = [NSArray arrayWithObject:exampleGoBoom];

Expand All @@ -114,30 +131,4 @@

nil
);
}



CONTEXT(DescribeMethodWithBeforeAndAfter){
__block int beforeCounter = 0;
__block int afterCounter = 0;

describe(@"The before each on describe",
beforeEach(^{
beforeCounter++;
}),

afterEach(^{
afterCounter++;
}),

it(@"calls before each before the example", ^{
[expect([NSNumber numberWithInteger:beforeCounter]) toBeEqualTo:[NSNumber numberWithInt:1]];
}),

it(@"calls after each after each example", ^{
[expect([NSNumber numberWithInteger:afterCounter]) toBeEqualTo:[NSNumber numberWithInt:1]];
}),
nil
);
}
}

0 comments on commit 62096ea

Please sign in to comment.