3

我正在嘗試使用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)我不想修改集成測試的生產代碼。

回答

1

單元測試

如果您要更換所有依賴與測試雙給定的類,因而從它的合作者隔離測試類,這將是一個單元測試。簡單地實例化一個實例進行測試,作爲協作者傳入你的測試雙打(模擬,存根等)。

集成測試

如果你想與測試雙組件補丁出一個或多個實例,以使系統進入所需的狀態爲一個集成測試,颱風提供了幾種方法。

可以修補出的組分如下:這種方法

MiddleAgesAssembly* assembly = [[MiddleAgesAssembly assembly] activate]; 

TyphoonPatcher* patcher = [[TyphoonPatcher alloc] init]; 
[patcher patchDefinitionWithSelector:@selector(knight) withObject:^id{ 
    Knight* mockKnight = mock([Knight class]); 
    [given([mockKnight favoriteDamsels]) willReturn:@[ 
     @"Mary", 
     @"Janezzz" 
    ]]; 

    return mockKnight; 

}]; 

[assembly attachPostProcessor:patcher]; 

Knight* knight = [(MiddleAgesAssembly*) factory knight] 

更多信息可在用戶指南的Integration Testing部分中找到。

模塊化

另外,您可以模塊化的組件,並與子類或替代實現,它提供某些種類的另一種實現方式,例如激活:在這個

UIAssembly *uiAssembly = [[UIAssembly new] 
    activateWithCollaboratingAssemblies:@[ 
     [TestNetworkComponents new], //<--- Patched for testing 
     [PersistenceComponents new]]; 

SignUpViewController* viewController = [uiAssembly signUpViewController]; 

更多信息方法可以在用戶指南的modularization section中找到。

+0

感謝您的快速回答:) – user3292998

1

如果要修補出所使用的故事板和初始化使用的plist集成組裝,那麼你可以通過調用使該程序集默認:

[yourAssembly makeDefault]; 

,並在你可以得到這個裝配您測試案例通過調用:

[yourAssembly defaultAssembly]; 

之後,你可以很容易地修補一些定義。在測試開始之前讓你的程序集默認很重要,所以也許應用程序委託將是一個很好的地方。這是probalby不是最好的解決方案,但它看起來像你想獲得一些全球性的組裝。

相關問題