iOS Mocking Class That Has Side Effects on Class Load
Asked Answered
Z

1

9

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];
Zinn answered 5/2, 2014 at 19:52 Comment(0)
P
3

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.

Plumbery answered 6/2, 2014 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.