Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stub CocoaLumberjack or NSLog with OCMockito

I can stub/verify a class method, but I'm having difficulty with defined macros. I'm trying to test that one of my methods calls DDLogInfo.

It's defined like so in the CocoaLumberjack source

#define DDLogInfo(frmt, ...)    LOG_MAYBE(LOG_ASYNC_ENABLED, LOG_LEVEL_DEF, DDLogFlagInfo,    0, nil, __PRETTY_FUNCTION__, frmt, ##__VA_ARGS__)

thanks!

like image 689
kreek Avatar asked Nov 09 '22 14:11

kreek


1 Answers

All standard DDLog macros call +[DDLog log:level:flag:context:file:function:line:tag:format:], so with OCMock, you would verify that DDLogInfo was called by:

- (void)testMethodCallsDDLogInfo {
    id mockDDLog = OCMClassMock([DDLog class]);

    [obj methodThatCallsDDLogInfo];

    OCMVerify([mockDDLog log:YES level:DDLogLevelAll flag:DDLogFlagInfo context:0 file:[OCMArg anyPointer] function:[OCMArg anyPointer] line:58 tag:[OCMArg any] format:[OCMArg any]]);
}

Unfortunately, with this strategy you must hard-code several values, as OCMock does not have a way to specify a generic non-pointer argument.

like image 53
Ryan Maloney Avatar answered Nov 14 '22 21:11

Ryan Maloney