2013-01-09 24 views
1

我想爲我創建的簡單UIDevice類別設置一些測試用例。 因爲這些方法是實例方法,所以我創建了一個模擬來模擬currentDevice行爲。 添加的方法基於name屬性,所以我創建了一個存根以在name屬性上返回一個假名稱。OCMock類別

存根運作良好,但如果我嘗試訪問我的其他方法,我得到非嘲弄的名稱屬性。

這裏是我的測試案例:

#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 

這裏是全碼:https://github.com/tiboll/TLLNameFromDevice/

(3評論說稱不工作)

沒有任何人有什麼想法?

+1

什麼是+ createNicelyMockedCurrentDevice和+ createMockedCurrentDevice?你可以發佈這些方法的代碼嗎? 我一直在嘲笑像[UIDevice currentDevice]這樣的單例對象,並且通常會將一個輔助方法放入我想要測試的類中,該方法返回生產代碼中的單例,然後使用部分模擬使其返回一個模擬對象單元測試。 – Greg

回答

0

要修復這些項目,請做以下事項。

UIDevice+UnitTests.m

添加以下方法:

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

刪除下面的方法:

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

    return invokeSupersequentNoParameters(); 
}  

而且從方法

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

刪除下面的語句
if (_name) 
    return _name;