OCMock returns values

I am trying to write a test for a method in which the result depends on the return value of NSDate timeIntervalSinceNow. I would like to specify the return value in my tests so that I can test certain scenarios.

It is very hard for me to get this OCMock object returning what I need. Here is my code:

id mock = [OCMockObject mockForClass:[NSDate class]]; NSTimeInterval t = 20.0; [[[mock stub] andReturnValue:OCMOCK_VALUE(t)] timeIntervalSinceNow]; STAssertEquals([mock timeIntervalSinceNow], 20.0, @"Should be eql."); 

This generates an "error: expected specifier-classifier-list before the error" typeof ".

Any thoughts? I'm new to ObjC, so any other tips related to it are really appreciated.

Thanks.

+4
source share
1 answer

Actually, this is a compiler error, not an OCMock error. This has something to do with how the macro OCMOCK_VALUE(t) works. It is defined as:

 #define OCMOCK_VALUE(variable) [NSValue value:&variable withObjCType:@encode(typeof(variable))] 

The typeof () directive is not part of C89, so make sure you set your compiler to use - std=gnu89 or std=gnu99 . According to Apple docs, if you install it on Compiler Default , this is equivalent to gnu89, which is also good.

This is probably the cause of your mistake.

+5
source

All Articles