How to stub a method that returns void

983 Views Asked by At

I hope my question is not too basic, as I am new to both obj-c and OCMockito!

I have a void method that I want to stub, so that it does not perform its actions when running a test.

My Method:

-(void)myVoidMethod { .. }

I would like to stub it in a way similar to this:

[given([mockDataManager saveChangesToCoreData])];

However if I dont specify a "willReturn" statement I get the following error: "Argument type 'void' is incomplete"

How can I achieve this in OCMockito?

2

There are 2 best solutions below

0
On BEST ANSWER

After getting more details from comments I decide to write the answer here.

IMO partial stubbing (spies) are bad practise. I used it two times in really big legacy project and I would like to change that to something cleaner at some point.

The same opinion is shared between other people. As quick solution you could follow advice from here - subclass and override the method.

0
On

Declaring dummy protocol where this method declared as returning id will fix compile error.

@protocol _dummyProtocol
    - (id)methodThatReallyReturnVoid;
@end

[[(id<_dummyProtocol>)[mockObject stub] methodThatReallyReturnVoid] andDo:^(NSInvocation *inv){}];