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?