I'm coming in without a rails/rspec background and trying out the 'specta' framework for unit testing on iOS. The one thing I don't understand is when to use specta's 'sharedExamplesFor'.
Is it just a test shared across all of your test suites that you can run before each test that's part of a group of similar test cases?
It is used to indicate common behaviour across multiple classes/components. For example you may have a couple of controllers which are both UITableView delegates. You could create a shared example that tests a given UIViewController conforms to the UITableViewDelegate protocol or that it responds to a given method. You can then use that shared behaviour across your two UIViewControllers.
sharedExamplesFor(@"Table controller", ^(NSDictionary *data) {
describe(@"Controller", ^{
it(@"is UITableViewDelegate", ^{
UIViewController *controller = data[@"controller"];
expect(controller).to.conformTo(@protocol(UITableViewDelegate));
});
});
});
describe(@"View controller 1", ^{
MyViewControllerOne *controllerOne = [[MyViewControllerOne alloc] init];
itBehavesLike(@"Table controller", @{@"controller" : controllerOne});
});
describe(@"View controller 2", ^{
MyViewControllerTwo *controllerTwo = [[MyViewControllerTwo alloc] init];
itBehavesLike(@"Table controller", @{@"controller" : controllerTwo});
});
This is an extremely trivial example and probably not something you would actually test for but hopefully shows the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With