2013-08-01 140 views
0

我是iOS開發新手,我正在爲我的代碼編寫單元測試。我需要訪問實例變量。是唯一的方法來創建一個類別,寫入getter方法,然後將其導入到我的測試文件?iOS單元測試實例變量

這裏是我的.m文件

//imports 
@implementation viewController{ 
    NSArray* a; 
    int b; 
    //other variables 
} 

//methods 

下面是測試文件

#import "ViewController_Tests.h" 
#import "ViewController.h" 

@implementation ViewController_Tests{ 
    ViewController *controller; 
} 

- (void)setUp { 
    [super setUp]; 
    controller = [[ViewController alloc] initWithNibName:nil bundle:nil]; 
} 

- (void)tearDown { 
    [super tearDown]; 
} 

- (void)test1 { 
    NSArray* a; 
    //I want to access the variables here! 

} 

@end 
+0

請提供更多詳細信息。實例變量屬於哪個類,您的應用程序委託? –

+0

它們是我創建的自定義類中的實例變量。在.m文件的@implementation塊中,我聲明瞭我想要訪問的實例變量。 – user1802143

+0

你可以在單元測試中創建該類的一個實例嗎? –

回答

1

我可以看到兩個解決方案,您可以使用:

1:使用UISpec框架來測試功能的視圖控制器,例如「斷言視圖控制器具有X個條目的表格視圖」(這是一個迴歸測試),並在具有完整GUI的模擬器中運行。

2:使用預編譯器標誌(如#ifdef UNIT_TESTS)打開對成員變量的訪問權限或添加訪問器方法,使用構建設置在其他預處理器標誌中定義UNIT_TESTS,因此它僅在單元測試中編譯。