Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Created February 27, 2018 02:54
Show Gist options
  • Save levantAJ/8630a319777889a60aa29b52a3b5a1cc to your computer and use it in GitHub Desktop.
Save levantAJ/8630a319777889a60aa29b52a3b5a1cc to your computer and use it in GitHub Desktop.
How to mock a static variable
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