Static variable rotates nile when executing tests

We have a static variable with a default value:

static NSDictionary *g_primaryKeyFieldName = NULL;

So that we initialize it with the correct value in the method didFinishLaunchingWithOptions

.

g_primaryKeyFieldName = [NSDictionary dictionary...];

Everything seems to be fine when you launch the application. However, when the tests run, the variable is initialized, but then somehow its value is set back to its initial value NULL

.

I have verified that the variable is not just set to nil

or freed, because if I set its default to something else:

static NSDictionary *g_primaryKeyFieldName = @"Some String";

Then the variable gets this value.

What could be the reason for this behavior?

+3


source to share


2 answers


The same thing happened to me and I found a possible reason. When file ( FCModel.m

) is included in both target applications and test targets, separate contexts (static variables, etc.) are created for calls from the application and from the tests. So the solution is this: make sure the file FCModel.m

has target membership only in MyFCApp

, and if you need to access your class FCModel

in tests, only refer to it through @testable import MyFCApp

.



+1


source


static NSDictionary* g_primaryKeyFieldName = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    g_primaryKeyFieldName = @{
      @"k": @100,
      @"u": @400,
      @"m": @40,
      };
});

      



-2


source







All Articles