0

I am trying to setup a OCMock to be verified.

I have a protocol, TaskManagerDelegate, that contains the following method,

- (void) addTasks:(NSArray * ) tasksToAdd;

After setting up my mock object like this,

id mockTaskManagerDelegate = [OCMockObject mockForProtocol:@protocol(TaskManagerDelegate)];

I assign the object to the class under test like this,

taskManager.Whatever = mockTaskManagerDelegate;

I call a method on my taskManager and then want to verify the addTasks method was called on the TaskManagerDelegate and that the array that was passed to it contains exactly one object.

So far I have used the OCMArg class to detect if a parameter is being passed in, but I am struggling to understand how to check that specific types are sent are sent to the mocks, or that the objects sent to mock pass certain tests (have a .count of exactly one for a example). I come from a C# background and would normally use Moq, where you can use lamda functions to do specific checks on parameters being sent to the mocked object.

Does any one know how to do this with OCMock or if for some conceptual reason it is not possible to do?

Thanks,

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
MuffinMan
  • 1
  • 1

2 Answers2

0

The features description on the OCMock site has this: ;-)

"If Objective-C blocks are available it is possible to check the argument with a block as follows:

[[mock expect] someMethod:[OCMArg checkWithBlock:^(id value) { /* return YES if value is ok */ }]];

Would that work for you? Are you in an environment where blocks aren't available?

Erik Doernenburg
  • 2,933
  • 18
  • 21
0

I had the same requirement and so created a category for it:

@implementation OCMArg (IsOfClass)

+ (id)isOfClass:(Class)aClass
{
    BOOL (^classCheck)(id) = ^BOOL(id obj) {
        return [obj isKindOfClass:aClass];
    };

    return [[OCMBlockConstraint alloc] initWithConstraintBlock:classCheck];
}

@end
Michael
  • 1,213
  • 6
  • 9