Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Variable Turns Nil When Running Tests

We have a static variable with a default value:

static NSDictionary *g_primaryKeyFieldName = NULL;

That we initialize it with a proper value in the didFinishLaunchingWithOptions method.

g_primaryKeyFieldName = [NSDictionary dictionary...];

Everything seems to be fine when running the app normally. However, when running the tests, the variable gets initialized but then somehow its value is set to the initial NULL value again.

I checked the variable is not being just set to nil or deallocated because if I set its default value to be something else:

static NSDictionary *g_primaryKeyFieldName = @"Some String";

Then that's the value the variable gets.

What could be causing this behavior?

like image 953
Fran Sevillano Avatar asked Aug 29 '26 04:08

Fran Sevillano


1 Answers

Same problem just happened to me and I found a possible reason. If a file (FCModel.m) is included in both application and test targets, separate contexts (static variables etc.) are created for calls from the application and from the tests. So the solution is: Make sure the FCModel.m file has target membership only in MyFCApp and if you need to access your FCModel class in tests, access it only via @testable import MyFCApp.

like image 66
Matej Vargovčík Avatar answered Aug 30 '26 22:08

Matej Vargovčík



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!