NISERuntimeFake
是一个 NSObject
分类,可以在运行时创建假对象。
它创建与真实对象不同行为的行为假对象。
您可以通过重写实现的实例方法来定义不同的行为。
//Creating fake object
YourClass *fakeObject = [YourClass fake];
//Overriding instance method
__block NSString *capturedString;
[fakeObject overrideInstanceMethod:@selector(doSomethingWithString:) withImplementation:^(YourClass *_self, NSString *string){
capturedString = string;
}];
//Use your fake object as you would normally use a real object
[fakeObject doSomethingWithString:@"Whatever"];
NSLog(@"%@", capturedString); //Output will be "Whatever"
//If you want to make new fake object with original implementation just create new one
fakeObject = [YourClass fake];
//Create fake object with protocol methods
YourClass *fakeObjectWithProtocol = [YourClass fakeObjectWithProtocol:@protocol(YourProtocol) includeOptionalMethods:YES];
//Create fake class
Class *fakeClass = [YourClass fakeClass];
//Create fake object with cusotm initializer
YourClass *fakeObject = [[fakeYourClass alloc] initWithWhatever:whatever];