2015-10-20 56 views
0

我对以下几点很好奇。由于我有在类扩展中声明的各种方法,是否可以使用XCTest对它们进行单元测试?课堂扩展中声明的单元测试方法

例如,给定包含方法foo一类扩展:

@interface FooClass() 

-(NSString*)foo; 

@end 

如何测试FOO:在一个测试类?

非常感谢您的帮助!

回答

2

您不需要测试内部方法,因为您可能在流程实施中更频繁地进行更改。测试需要来自* .h文件,但如果您需要,您可以创建测试类别。你也可以使用运行时(例如 - performSelector)

RSFooClass.h

#import <Foundation/Foundation.h> 


@interface RSFooClass : NSObject 
@end 

RSFooClass.m

#import "RSFooClass.h" 


@implementation RSFooClass 

- (NSString *)foo { 
    return @"Hello world"; 
} 

- (NSInteger)sum:(NSInteger)a with:(NSInteger)b { 
    return a + b; 
} 

@end 

RSFooClassTest.m

#import <XCTest/XCTest.h> 
#import "RSFooClass.h" 


@interface RSFooClass (Testing) 

- (NSString *)foo; 
- (NSInteger)sum:(NSInteger)a with:(NSInteger)b; 

@end 


@interface RSFooClassTest : XCTestCase 

@property (strong, nonatomic) RSFooClass *foo; 

@end 


@implementation RSFooClassTest 

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

    self.foo = [[RSFooClass alloc] init]; 
} 

- (void)testFoo { 
    NSString *result = [self.foo foo]; 
    XCTAssertEqualObjects(result, @"Hello world"); 
} 

- (void)testSumWith { 
    NSInteger a = 1; 
    NSInteger b = 3; 
    NSInteger result = [self.foo sum:a with:b]; 
    NSInteger expected = a + b; 
    XCTAssertEqual(result, expected); 
} 

@end