你可以测试,既使用方法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 *)字符串? –