我正在尝试使用 KIF 编写集成测试。我的问题是:
如何为特定视图控制器注入存根、模拟或虚假依赖项?
每个使用数据模型、http 客户端、存储管理器等依赖项的视图控制器都来自 ModelAssembly、ApplicationAssembly、ManagerAssmebly。
在故事板上,对于登录视图,我有一个关键路径,包含值“login ViewController”。
创建视图控制器:
ViewControllersAssembly.h
@interface ViewControllersAssembly : TyphoonAssembly
@property (nonatomic, strong) ModelAssembly *modelAssembly;
- (id)loginViewController;
@end
ViewControllersAssembly.m
@implementation ViewControllersAssembly
- (UIViewController *)loginViewController {
return [TyphoonDefinition withClass:[LoginViewController class] configuration:^(TyphoonDefinition *definition) {
[definition injectProperty:@selector(userModel) with:[self.modelAssembly userModel]];
}];
}
UserModel 有登录方法
- (RACSingnal*)loginWithEmail:(NSString*)email password:(NSString*)password;
现在在集成测试目标中我有这样的类:
登录测试.h
@interface LoginTests : KIFTestCase
@property (nonatomic, strong) UserModel *fakeUserModel;
@end
登录测试.m
@implementation LoginTests
- (void)beforeAll {
self.fakeDataModel = [self mockDataModel];
}
- (void)testLogin {
[self.fakeDataModel mockNextResponse:[RACSignalHelper getGeneralErrorSignalWithError:[[NSError alloc] initWithDomain:@"http://some.com" code:452 userInfo:nil]]];
[tester waitForViewWithAccessibilityLabel:@"loginScreen"];
[tester enterText:@"[email protected] /cdn-cgi/l/email-protection" intoViewWithAccessibilityLabel:@"emailAdress"];
[tester enterText:@"asd123" intoViewWithAccessibilityLabel:@"password"];
[tester tapViewWithAccessibilityLabel:@"loginButton"];
[tester tapViewWithAccessibilityLabel:@"OK"];
// for example error code 542 we should display alert with message "User Banned"
// now somehow check that UIAlertView localizedDescription was "User Banned"
}
- (FakeUserModel *)mockUserModel {
ModelAssembly *modelAssembly = [[ModelAssembly assembly] activate];
TyphoonPatcher *patcher = [[TyphoonPatcher alloc] init];
[patcher patchDefinitionWithSelector:@selector(userModel) withObject:^id{
return [FakeUserModel new];
}];
[modelAssembly attachDefinitionPostProcessor:patcher];
return [modelAssembly userModel];
}
假用户模型是覆盖用户模型类的类,增加了为下一个调用的请求存根响应的可能性。
该解决方案不起作用。
我应该如何以及在哪里传递 FakeUserModel?
1)我想访问注入的实例
2)注入的实例必须是FakeUserModel类型,仅在集成测试目标中。
3)我不想修改集成测试的生产代码。