2015-04-06 54 views
0

我已經聲明瞭一個方法叫做按鈕狀態-(int)buttonstate;沒有得到值xctest

xctest.m當我instansiate我的viewController和分配給一個int值我我得到錯誤的值。

-(int) buttonstate { 

    if ([searchButton isEnabled]) { 
     j = 1; 
    } 
    else 
     j = 0; 

    NSLog(@" j value is %d",j); 

    return j; 
} 

而在xctest .m文件

-(void) testwithoutData { 
    myapiViewController *apiViw = [[myapiViewController alloc]init]; 


    int kn = [apiViw buttonstate]; 

    NSLog(@"the value is %ld",(long)kn);   
} 
+0

這兩種方法都在同樣的看法?並且你想在其他控制器中傳遞狀態值? – 2015-04-06 09:16:41

+0

如果UITextField包含文本,那麼uiButton將啓用,值將爲1,我需要在xctest.m文件中的j值我得到的值爲0如果我運行測試之前視圖分配的值是1後,當我請參閱(int kn)它是0 – 2015-04-06 09:21:06

+0

必須在調用buttonstate以在運行測試用例時看到值1之前,在testwithoutData中啓用serachButton。 – 2015-04-06 10:11:33

回答

0

這是我曾嘗試 -

在ViewController.h

@interface ViewController : UIViewController{ 
    @public int index; 
} 

在ViewController.m

​​ 種

在ViewControllerTests.m

@interface ViewControllerTests : XCTestCase 
@property (nonatomic,strong) ViewController *vcToTest; 
@end 

@implementation ViewControllerTests 

- (void)setUp { 
    [super setUp]; 
    // Put setup code here. This method is called before the invocation of each test method in the class. 
    self.vcToTest = [[ViewController alloc] init]; 
} 

- (void)testExample { 
    // This is an example of a functional test case. 
    XCTAssert(YES, @"Pass"); 
    [self.vcToTest drawCircle]; 
    NSLog(@"Test is fine. Value %d",self.vcToTest->index); 
} 

測試結果 - enter image description here

+0

當我打電話畫圓的方法之前登錄索引值,我得到的指數值0 – 2015-04-06 10:19:18

+0

請詳細說明,以及如何獲得10 – 2015-04-06 10:28:49

+0

爲了顯示我在ViewControllerTests畫圓法設置索引值,我稱這種方法只是記錄指數前值在testExample。我想沒有調用畫圓,但那個時候我得到的索引值0。當我調試,我發現的ViewController該實例得到改變和指標值的變化,從10到0。我呼籲在視圖控制器viewDidLoad中畫圓的方法。因此,我建議您先調用searchbutton啓用方法,然後嘗試檢查要打印的值。希望這可以幫助。檢查附加的測試結果截圖。 – 2015-04-06 10:39:31