Skip to content

Instantly share code, notes, and snippets.

@noahmiller
Last active March 5, 2016 21:55
Show Gist options
  • Save noahmiller/8940573 to your computer and use it in GitHub Desktop.
Save noahmiller/8940573 to your computer and use it in GitHub Desktop.
An example of using shared Specta behavior
SharedExamplesBegin(OrderedCollectionBehavior)
// A set of shared test cases are defined here under the key name
// "an ordered collection".
//
// The dictionary param is the only means of providing data from
// the shared examples caller to the set of shared examples.
// In this case pass in the collection class to be tested.
sharedExamplesFor(@"an ordered collection", ^(NSDictionary* data)
{
// Pull the class to test out of the data object
Class collectionClass = data[@"collectionClass"];
it(@"should be empty when first created",
^{
id collection = [[collectionClass alloc] init];
expect([collection count]).to.equal(0);
});
it(@"should allowing initializing with a contained object",
^{
expect(
^{
(void) [[collectionClass alloc] initWithObject:@"foo"];
}).notTo.raise(nil);
});
it(@"should reply to first with the object provided during init",
^{
NSString* object = @"foo";
id collection = [[collectionClass alloc] initWithObject:object];
expect([collection firstObject]).to.equal(object);
});
});
SharedExamplesEnd
// Here's one instance of a spec making use of the shared behavior.
// Test that an array behaves like an ordered collection.
SpecBegin(NSArray)
describe(@"shared array behavior",
^{
NSDictionary* data = @{@"collectionClass": [NSArray class]};
itShouldBehaveLike(@"an ordered collection", data);
});
SpecEnd
// And here's a second instance of a spec making use of
// the shared behavior. Test that an ordered set behaves
// like an ordered collection.
SpecBegin(NSOrderedSet)
describe(@"shared ordered set behavior",
^{
NSDictionary* data = @{@"collectionClass": [NSOrderedSet class]};
itShouldBehaveLike(@"an ordered collection", data);
});
SpecEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment