views:

216

answers:

1

Hey all, I'm trying to write a test for a method where the output depends on an NSDate's timeIntervalSinceNow return value. I'd like to specify the return value in my tests so I can test certain scenarios.

I'm having a really hard time getting this OCMock object returning what I'd like. Here's 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 a "error: expected specifier-qualifier-list before 'typeof" error.

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

Thanks.

+2  A: 

Actually, it is a compiler error, not an OCMock error. This has something to do with the way the OCMOCK_VALUE(t) macro 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 have set your compiler to use -std=gnu89 or std=gnu99 flag. According to the Apple docs, if you set it to Compiler Default this is equivalent to gnu89, which is fine also.

This is probably the cause of your error.

Johannes Rudolph
This is defined by the GCC_C_LANGUAGE_STANDARD in the target's build properties, correct? If so, my unit test bundle already has C99 as the value.This is for an iPhone app, on Xcode 3.1.4, if that makes a difference.
D. Pfeffer
sorry, I got that wrong. You need to set it to GNU99.
Johannes Rudolph
That worked! Thank you very much.
D. Pfeffer
I found that with the default project settings `__typeof__` was defined but not `typeof` so I created a header called `SpecHeler.h` that I include before OCMock in each of my specs (I'm using [Cedar](http://github.com/pivotal/cedar)) with the following in it `#define typeof __typeof__`
Wes