Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Mocking Class That Has Side Effects on Class Load

We're trying to create a unit test (with OCMock although, open to other frameworks) that mocks a class that on class load has a side effect.

We have a tracking class that wraps calls to other tracking libraries like Flurry.

Many of these other tracking libraries (specifically, Flurry) execute code on class initialization.

The unit test fails as the code can't execute in a unit test environment. Ideally, we would like to replace the Flurry class with a mock/stub.

- (void) testConstruction {
    [Flurry class];
}

When this code is called it attempts to use SCNetworkReachability and receives exceptions...

How can we stub/mock out calls to Flurry that has a static implementation like the following?

[Flurry setAppVersion:@"1.0"];
[Flurry setCrashReportingEnabled:NO];
like image 469
Gary Rudolph Avatar asked Feb 05 '14 19:02

Gary Rudolph


1 Answers

I would remove Flurry from the build configuration I'm using for test (remove it from the Framework Search path / library search path). I'd create my own dummy Flurry by grabbing the Flurry header file and creating my own implementation with empty methods. Finally, if I care that Flurry is being called, I would use OCMock to mock my fake class.

like image 55
Ben Flynn Avatar answered Sep 18 '22 06:09

Ben Flynn