Problems when mocking UIKit objects

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Problems when mocking UIKit objects

Postby jdortiz » 27 Nov 2014, 20:53

I have been testing my view controllers for a while. After all, that is part of what I teach others in the advanced training :-) and also I believe that it is the right thing to do.

In order to do these tests I have used OCMock to check interaction with their dependencies, like NSUserDefaults or UITableView. I have been experimenting random crashes when running my tests, and their number increased when I moved to Xcode 6 and OCMock v3. I have been doing some research and I have written down my results here: http://powwau.com/blog/2014/11/debug-exc-bad-address-in-unit-tests/ As you will see many were my fault, but there is one at the end of the article that crashes 1 out of 10 times at least and that I cannot understand.

I would appreciate any comments or corrections as well as your experience on the subject. Thank you!

Best regards,

Jorge Ortiz
jdortiz
 
Posts: 2
Joined: 27 Nov 2014, 20:49

Re: Problems when mocking UIKit objects

Postby erik » 29 Nov 2014, 15:58

Thanks for writing this up. I'd say that even the first one (non-retained object passed to andReturn) should be considered a bug in OCMock. I suspect that this is something that's related to the automatic distinction between values and objects introduced in OCMock 3. (Remember, before you had to write andReturn: or andReturnValue: depending on what the method signature was.) I'll look into this.

For the second case, the sut is a UIViewController instance, rigth? How do you create it in your tests?
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany

Re: Problems when mocking UIKit objects

Postby jdortiz » 06 Dec 2014, 16:11

Hi Erik:

Thank you for your reply. Sorry for the delay in replying, I thought I would get an email when somebody replied to my post. My fault.

Answering your question, the sut is indeed a subclass of UIViewController and I am using Storyboards, so this is how I create the sut:
Code: Select all
- (void) createSut {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    sut = [storyboard instantiateViewControllerWithIdentifier:viewControllerID];
}

Let me know if you need more data. Glad to help to be helped :-)

Best regards,

Jorge Ortiz
jdortiz
 
Posts: 2
Joined: 27 Nov 2014, 20:49

Re: Problems when mocking UIKit objects

Postby djackson » 12 Dec 2014, 04:04

erik wrote:I'd say that even the first one (non-retained object passed to andReturn) should be considered a bug in OCMock. I suspect that this is something that's related to the automatic distinction between values and objects introduced in OCMock 3. (Remember, before you had to write andReturn: or andReturnValue: depending on what the method signature was.) I'll look into this.


I think we're encountering the same (or a related) issue, any update?

I'm passing an object into andReturn(), but the object is garbage when it's returned from the mock object, and our test crashes in some exciting way. It's only been happening on 64-bit destinations, which is a little strange.

I just took a look into the andReturn() macro. I had no idea that you could use braces to turn statements into a r-value. That's kind of neat.

This crashes:
Code: Select all
OCMExpect([...]).andReturn(@[obj]);


This does not:
Code: Select all
NSArray *array = @[obj];
OCMExpect([...]).andReturn(array);

Nor does this:
Code: Select all
[OCMExpect([...]) andReturn:@[obj]]
djackson
 
Posts: 1
Joined: 12 Dec 2014, 03:59


Return to OCMock



cron