你可以測試,既使用方法swizzling或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 *)string? –
@ user2404543是的,只是想要一個健全的檢查,以確保它不會流浪,如果我決定以後添加功能 –
林不知道我明白了..爲什麼不能只調用[CustomClass方法:字符串對象:無] ;從+(無效)方法:(NSString *)字符串? –