我正在嘗試使用KIF編寫集成測試。我的問題是:如何使用Typhoon爲集成測試注入僞造,殘根或模擬依賴關係
如何注入特定視圖控制器的殘留,模擬或僞造依賴項?
使用依賴關係(如數據模型,http客戶端,商店管理器等)的每個視圖控制器來自ModelAssembly,ApplicationAssembly,ManagerAssmebly。
在故事板上,對於登錄視圖,我有一個鍵路徑,其中包含值「loginViewController」。
創建視圖控制器:
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]];
}];
}
我們erModel有沒有辦法登錄
- (RACSingnal*)loginWithEmail:(NSString*)email password:(NSString*)password;
現在,在集成測試目標,我有類,如:
LoginTests.h
@interface LoginTests : KIFTestCase
@property (nonatomic, strong) UserModel *fakeUserModel;
@end
LoginTests.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]" 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];
}
FakeUs erModel是覆蓋UserModel類的類,爲下一個調用的請求增加了存根響應的可能性。
該解決方案不工作。
如何以及在哪裏我應該通過FakeUserModel?
1)我想訪問注入實例
2)注入實例必須是類型FakeUserModel,這是隻有在集成測試目標的。
3)我不想修改集成測試的生產代碼。
感謝您的快速回答:) – user3292998