Created
February 27, 2018 02:54
-
-
Save levantAJ/8630a319777889a60aa29b52a3b5a1cc to your computer and use it in GitHub Desktop.
How to mock a static variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With my case how I mock the static variable and I hope this can help you, here is my workaround: | |
For example, I have a static `count` value in production code: | |
``` | |
@implementation MyClass | |
static int _count = 0; | |
- (int)viewDidAppearCount { | |
return _count; | |
} | |
- (void)setViewDidAppearCount:(int)count { | |
_count = count; | |
} | |
@end | |
``` | |
In my test class, I public the set method | |
``` | |
@interface MyClass (Testing) | |
- (void)setViewDidAppearCount:(int)count | |
@end | |
``` | |
Then now I'm able to mock the static variable: | |
``` | |
- (void)testMyMethod { | |
//Given: | |
[self.sut setViewDidAppearCount:aValue]; | |
//When: | |
//... | |
//Then: | |
//... | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment