我正在尝试使用 Typhoon 编写 XCTest 并注入模拟依赖项。
这是我的代码ViewController
:
- (instancetype)init {
self = [super init];
MDMainAssembly *assembly = (MDMainAssembly *) [TyphoonComponentFactory defaultFactory];
self.alertManager = [assembly alertManager];
return self;
}
这是我尝试更改注入的方式:
self.mockedAlertManager = mock([MDAlertManager class]);
MDMainAssembly *assembly = [MDMainAssembly assembly];
TyphoonComponentFactory *factory = [TyphoonBlockComponentFactory factoryWithAssembly:assembly];
TyphoonPatcher *patcher = [[TyphoonPatcher alloc] init];
[patcher patchDefinition:[assembly alertManager] withObject:^id {
return self.mockedAlertManager;
}];
[factory attachPostProcessor:patcher];
但是测试失败,因为无法将此工厂设置为默认值。我在AppDelegate
工厂配置:
TyphoonComponentFactory *factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
[MDMainAssembly assembly],
]];
[factory makeDefault];
如何摆脱这种局面?