1

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?

edoDev
  • 551
  • 1
  • 4
  • 20
  • Can you explain you case more. Why do you want to stub void method? – Eugen Martynov Jun 02 '14 at 13:56
  • I would like to prevent the insides (the functionality) of that specific method from running. The method I would like to stub, is called from within the method I am testing, but I would like to not execute the instructions inside it. Does that make sense? – edoDev Jun 04 '14 at 07:28
  • In general I would avoid partial stubbing. It is good smell that design is not right. – Eugen Martynov Jun 04 '14 at 08:35
  • You can find some solutions here: https://github.com/jonreid/OCMockito/issues/38 – Eugen Martynov Jun 04 '14 at 08:36

2 Answers2

0

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.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
0

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){}];
Oleksiy Ivanov
  • 2,454
  • 15
  • 21