2013-03-22 56 views
0

我刚启动OCUnitTesting。我不认为我的代码是错误的,但问题是,可能是一个微小的失误我的地方,一路上做。我有一个函数STAssertTrue没有像我期望的那样返回

- (BOOL)tooManyCouplesForER:(NSMutableArray *)startfield 
{ 
    NSLog(@"Size %i", [startfield count]); 
    if ([startfield count] > 7) { 
     return true; 
    } else{ 
     return false; 
    } 
} 

我的ViewController StartfieldTableViewController内。

我包括单元测试到我的项目,并在AppTests.h我做

#import "StartfieldTableViewController.h" 

@property (strong, nonatomic) StartfieldTableViewController *start; 

AppTests.m我写了一个测试

- (void)testTooManyCouplesForER 
{ 
    NSMutableArray *testField = [[NSMutableArray alloc] initWithObjects:@"1", @"1", @"1", @"1", @"1", @"1", @"1", @"1", @"1", @"1", nil]; 
    BOOL sum = [self.start tooManyCouplesForER:testField]; 

    NSLog(@"BOOL = %@\n", (sum ? @"YES" : @"NO")); 
    STAssertTrue(sum, @"This should have returned true, but it returned false"); 
} 

,所以我期望测试通过,因为我给了它10个元素的数组。它失败。所以我做了NSLog,在测试中它确实是NO,但是在运行时测试函数,使用相同的testField-array,这是事实。

感谢您的帮助。

+0

您何时/何时初始化启动属性? – 2013-03-22 15:42:04

回答

0

在单元测试,检查你执行

BOOL sum = [self.start tooManyCouplesForER:testField]; 

之前self.start不为零。

STAssertNotNil(self.start,@"You forgot to set the controller") 
    BOOL sum = [self.start tooManyCouplesForER:testField]; 

我敢打赌它没有在单元测试中初始化,所以总和为零,测试失败。

+0

* harrumph * ......你是对的..谢谢! – Nareille 2013-03-22 15:46:31

相关问题