Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use specta's 'sharedExamplesFor'?

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?

like image 970
Willam Hill Avatar asked Feb 01 '14 16:02

Willam Hill


1 Answers

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.

like image 98
JFoulkes Avatar answered Sep 27 '22 18:09

JFoulkes