Reputation: 3051
I'm struggling with a testcase that uses OCMock. So far I used OCMock in a few projects and it always work as i thought. I have the following method that tests that [self setNeedsDisplay]
is called when I set a property called image
.
- (void)testSetTapColorCallsDrawRect
{
SMColorButton *button = [[SMColorButton alloc] initWithImage:nil];
id mock = [OCMockObject partialMockForObject:button];
// Exptect setNeedsDisplay.
[[mock expect] setImage:OCMOCK_ANY];
[[mock expect] setNeedsDisplay];
button.image = [UIImage imageNamed:@"Button.png"];
[mock verify];
}
And the setter for the image
property looks like this.
- (void)setImage:(UIImage *)anImage
{
// Set the image if it changed.
if (anImage != image) {
image = anImage;
[self setNeedsDisplay];
}
}
You can see [self setNeedsDisplay]
is called in the setter. But when I run the tests I get the following error:
OCPartialMockObject[SMColorButton]: expected method was not invoked: setNeedsDisplay
It looks as if the method was not called. But if I set a breakpoint the the setter method, it stops and shows me that the line was executed by the application.
Can you help me out? I don't see what I'm doing wrong...
Upvotes: 0
Views: 1122
Reputation: 17762
The problem is that you're mocking the method you're testing, setImage:
. That expectation intercepts the call, so it never follows the code path in the class under test. Try changing your test to:
- (void)testSetTapColorCallsDrawRect
{
SMColorButton *button = [[SMColorButton alloc] initWithImage:nil];
id mock = [OCMockObject partialMockForObject:button];
[[mock expect] setNeedsDisplay];
button.image = [UIImage imageNamed:@"Button.png"];
[mock verify];
}
Upvotes: 2