2

I'm trying to set up some test cases for a simple UIDevice category I've created. Since these methods are instance methods, I have created a mock to emulate the currentDevice behavior. The added methods are based on the name attribute so I've created a stub to return a fake name on the name attribute.

The stub works well but if I try to access my other methods I get the non-mocked name attribute.

here is my test case:

#import "UIDevice_NameFromDevice_Tests.h"

#import <OCMock/OCMock.h>

#import "UIDevice+UnitTests.h"
#import "UIDevice+NameFromDevice.h"

@implementation UIDevice_NameFromDevice_Tests

-(void)setUp
{
    self.firstName = @"John";
    self.lastName = @"Doe";
    self.fullName = [NSString stringWithFormat:@"%@ %@",self.firstName,self.lastName];

    self.mockedDevice = [UIDevice createNicelyMockedCurrentDevice];
}

-(void)tearDown
{
    self.firstName = nil;
    self.lastName = nil;
    self.fullName = nil;
    [UIDevice releaseInstance];
}


#pragma mark UIDevice mock checks

-(void)test_mockedDevice_shouldBeEqualToCurrentDevice
{
    self.mockedDevice = [UIDevice createMockedCurrentDevice];

    STAssertEqualObjects(self.mockedDevice, [UIDevice currentDevice], nil);
}

-(void)test_stubedDeviceName_shouldBeEqualToCurrentDeviceName
{


    NSString *format = @"%@ %@'s iPhone";
    NSString *fakeiPhoneName = [NSString stringWithFormat:format,self.firstName,self.lastName];

    [[[self.mockedDevice stub] andReturn:fakeiPhoneName] name];

    STAssertEqualObjects([self.mockedDevice name], [[UIDevice currentDevice] name], nil);
    STAssertEqualObjects([[UIDevice currentDevice] name], fakeiPhoneName, nil);

}

#pragma mark test langages

-(void)test_BasicENName_shouldReturnValues
{
    NSString *fakeiPhoneName = [NSString stringWithFormat:@"%@'s iPhone", self.fullName];

    [[[self.mockedDevice stub] andReturn:fakeiPhoneName] name];

    NSLog(@"name : %@",[[UIDevice currentDevice] name]);
    NSLog(@"fullname : %@",[[UIDevice currentDevice] fullNameFromDevice]);
    NSLog(@"firstname : %@",[[UIDevice currentDevice] firstName]);
    NSLog(@"lastname : %@",[[UIDevice currentDevice] lastName]);

    STAssertEqualObjects([self.mockedDevice name], [[UIDevice currentDevice] name], nil);
    STAssertEqualObjects([[UIDevice currentDevice] name], fakeiPhoneName, nil);
//    STAssertEqualObjects([[UIDevice currentDevice] fullNameFromDevice], self.fullName, nil);
//    STAssertEqualObjects([[UIDevice currentDevice] firstName], self.firstName, nil);
//    STAssertEqualObjects([[UIDevice currentDevice] lastName], self.lastName, nil);
}

@end

here is the full code: https://github.com/tiboll/TLLNameFromDevice/

(the 3 commented asserts don't work)

does anyone have any idea?

  • 1
    What are +createNicelyMockedCurrentDevice and +createMockedCurrentDevice? Can you post the code for those methods? I've had trouble mocking singleton objects like [UIDevice currentDevice] and usually put a helper method into the class I want to test that returns the singleton in production code and then use a partial mock to make it return a mocked object for my unit testing. – Greg Jan 10 '13 at 15:42

1 Answers1

0

To fix these items, Please do following items.

In UIDevice+UnitTests.m

Add below method:

+(id)createPartialMockedCurrentDevice
{
    _mockedCurrentDevice = [OCMockObject partialMockForObject:[UIDevice currentDevice]];
    return _mockedCurrentDevice;
}

remove Below Method:

+(UIDevice *)currentDevice
{
    if (_mockedCurrentDevice)
        return _mockedCurrentDevice;

    return invokeSupersequentNoParameters();
}    

And remove below statements from method

-(NSString *)fullNameFromDevice in class UIDevice+NameFromDevice.m

if (_name)
    return _name;
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108