JFoulkes
JFoulkes

Reputation: 2429

OCMock stub isSelected property

I am trying to stub the selected property on UIButton. The getter is defined as:

@property (nonatomic, getter=isSelected) BOOL selected;

My stub looks like this:

[[[button stub] andReturnValue:OCMOCK_VALUE(TRUE)] isSelected];

I receive the following error when I run the test:

Return value does not match method signature; signature declares 'c' but value is 'i'.

I think this is something to do with the getter=isSelected part but not sure what's going on

Is it possible to stub this type of getter?

Upvotes: 3

Views: 2718

Answers (2)

Erik Doernenburg
Erik Doernenburg

Reputation: 3016

This is annoying. The problem is that passing TRUE to OCMOCK_VALUE results in the creation of a value of type integer. The message you get is OCMock's way of saying that the method/property you want to stub is a boolean but you provided an integer.

You can force the creation of a an actual boolean value with either of the following:

[[[button stub] andReturnValue:OCMOCK_VALUE((BOOL){TRUE})] isSelected];

[[[button stub] andReturnValue:@YES] isSelected];

By the way, a similar problem occurs with different number types but unfortunately fixing this in OCMock isn't trivial. See here for example: https://github.com/erikdoe/ocmock/pull/58.

Upvotes: 4

JFoulkes
JFoulkes

Reputation: 2429

This doesn't answer my question but incase anyone else stumbles across this problem a workaround is to do a partial mock of an actual instance of UIButton.

UIButton *button = [[UIButton alloc] init];
button.selected = TRUE;
id mockButton = [OCMockObject partialMockForObject:button];

Upvotes: 0

Related Questions