anthony hall
anthony hall

Reputation: 73

use OCMockito to test category class method

I have a UIColor category that has a class method

+(UIColor *)appropriateTextColorForBackground:(UIColor *)background
{
    //...get brightness value 
    if (brightness > 127.5f)
        return [UIColor blackColor];
    else
        return [UIColor whiteColor];
}

I want to test with OCMockito using this in my test class

-(void)testAppropriateColorWithBlackShouldReturnWhiteColor
{
    Class color = mockClass([UIColor class]);
    [color appropriateTextColorForBackground:black];
    assertThat([color testColorWithColor:black], is([UIColor whiteColor]));
}

but I get the error

test failure: -: *** -[NSProxy doesNotRecognizeSelector:appropriateTextColorForBackground:] called!

what am I missing? it seems that this should work

Upvotes: 1

Views: 247

Answers (1)

Eugen Martynov
Eugen Martynov

Reputation: 20140

I agree with Bryan, that you don't need mock here since you want to test your implementation of category method. As example:

-(void)testAppropriateColorWithBlackShouldReturnWhiteColor
{
    UIColor *appropriateColor = [color appropriateTextColorForBackground:black];
    assertThat(appropriateColor, is(equalTo([UIColor whiteColor])));
}

You also probably want to have similar test for opposite color. I would probably go further and will use colors that are on the border of change for you brightness calculation (instead of black and white). However someone (not me) could argue that this will expose implementation details which is usually thing to avoid while writing unit test.

Upvotes: 1

Related Questions