0

I would like to test method, that reads and writes value from NSUserDefaults. Here is how read method looks like:

+ (NSDate *)initialImportDate
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:STBInitialImportDateKey];
}

I want to stub [NSUserDefaults standardUserDefaults] and I was doing before and it worked perfectly. Assuming that we are stubbing class method, we have clear class stubs.

it(@"should load date from NSUserDefaults", ^{
     NSDate *expectedDate = [NSDate date];
     id mockNSDefaults = [NSUserDefaults nullMock];
     [NSUserDefaults stub:@selector(standardUserDefaults) andReturn:mockNSDefaults];

     [[mockNSDefaults should] receive:@selector(objectForKey:) andReturn:expectedDate withArguments:@"ImportDate"];

     [[[Importer initialImportDate] should] equal:expectedDate];

     [KWMock clearStubs];
});

When using Kiwi 2.2.4, I get error at the last line, that there is no class method, only instance one. As far I have no stub object, is it legal to got this way?

[[NSUserDefaults class] clearStubs];
Gurnetko
  • 76
  • 4

1 Answers1

0

Yes, you can safely call clearStubs on the class object, as the objetive-c runtime dispatches messages to classes like for regular objects, assuming of course the selector exists on the destination class.

Cristik
  • 30,989
  • 25
  • 91
  • 127