Reputation: 4914
I wrote a mock Coredata
manager in order to test some classes in unit tests.
I have about 10 classes that get's NSManagedObjectContext
from a class called DatabaseManager
. I have decided if unit tests are running, don't deal with actual Coredata NSManagedObjectContext
but redirect to Mock Coredata
Class to get the NSManagedObjectContext
.
func getContext() -> NSManagedObjectContext {
if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil
{
return persistentContainer.viewContext
}
else
{
return MockDatabaseController.instance.managedObjectContext()
}
}
This works pretty well in Unit tests and debugging and when distributed thru adhoc too.
But my concern is if it ever fails to get the correct value from ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"]
app will probably be useless.
How viable is it to use ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"]
in production code ?
Upvotes: 1
Views: 1188
Reputation: 23701
I would use Swift conditional compilation along with -D flags passed in the build arguments to make sure that the code was only active in test environments and never had the opportunity to make it into production.
Upvotes: 2