Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top