'Avoid run part of code when use Unit test
I want that don't execute part of my code when I run the app for Unit testing, the problem is one my functions is save one object in BD and I want avoid this.
api.getUsers(completion:{(objects)
//In this response I save the object in BD
})
Solution 1:[1]
I don't think you really want to conditionally execute something at runtime based if you are running unit tests or not. That would be a terrible idea IMHO.
What you probably want in your unit tests is to create a mock object of your api class and then inject the mocked version into the class you are testing.
Solution 2:[2]
Does your operating system has environmental values? In React, I solve this with checking process.env.NODE_ENV !='test'
Solution 3:[3]
You can set an on-launch argument like isTest to YES in your scheme properties (section Test).
And, in your function use this :
BOOL isTest = [[NSUserDefaults standardUserDefaults] boolForKey:@"isTest"];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Kiszuriwalilibori |
| Solution 3 | marshallino16 |
