두 가지 방법을 혼돈스럽게하거나 OCMock을 사용하여 테스트 할 수 있습니다. 방법 스위 즐링으로
, 우리가 테스트 구현 파일에 다음과 같은 변수를 선언 첫째 :
static NSString *passedString;
static id passedObject;
그럼 우리가 (테스트 클래스에서) 스텁 메소드를 구현하고, 스위 즐링로 이동합니다
+ (void)stub_method:(NSString *)string object:(id)object
{
passedString = string;
passedObject = object;
}
- (void) test__with_method_swizzling
{
// Test preparation
passedString = nil;
passedObject = [NSNull null];// Whatever object to verify that we pass nil
Method originalMethod =
class_getClassMethod([CustomClass class], @selector(method:object:));
Method stubMethod =
class_getClassMethod([self class], @selector(stub_method:object:));
method_exchangeImplementations(originalMethod, stubMethod);
NSString * const kFakeString = @"fake string";
// Method to test
[CustomClass method:kFakeString];
// Verifications
STAssertEquals(passedString, kFakeString, nil);
STAssertNil(passedObject, nil);
method_exchangeImplementations(stubMethod, originalMethod);
}
그러나 우리는 훨씬 간단한 방법으로 OCMock과 같은 작업을 수행 할 수 있습니다
- (void) test__with_OCMock
{
// Test preparation
id mock = [OCMockObject mockForClass:[CustomClass class]];
NSString * const kFakeString = @"fake string";
[[mock expect] method:kFakeString object:nil];
// Method to test
[CustomClass method:kFakeString];
// Verifications
[mock verify];
}
내가 돈 ' 당신의 질문을 정말로 이해하지 못합니다. + (void) 메서드 호출 : (NSString *) 문자열 개체 : (id) + (void) 메서드에서 개체 : (NSString *) 문자열? –
@ user2404543 네, 나중에 기능을 추가하기로 결심한다면 이탈 확인을 원합니다. –
Im 얻을 수 없다는 것을 확신하지 못합니다. 왜 [CustomClass method : string object : nil]이라고 부르지 않습니까? ; + (void) 메서드에서 : (NSString *) 문자열? –