我正在尝试编写XCTest并使用Typhoon注入模拟依赖。
以下是我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
factory中配置:
TyphoonComponentFactory *factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
[MDMainAssembly assembly],
]];
[factory makeDefault];
如何摆脱这种情况?
答案 0 :(得分:2)
我们为有限数量的案例创建了defaultFactory功能。主要是:
虽然您可以在测试中使用它,但我们建议您为每次测试运行创建并销毁Typhoon容器。为避免重复,您可以按如下方式创建方法:
@implementation IntegrationTestUtils
+ (TyphoonComponentFactory*)testAssembly
{
TyphoonComponentFactory* factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
[MyAppAssembly assembly],
[MyAppKernel assembly],
[MyAppNetworkComponents assembly],
[MyAppPersistenceComponents assembly]
]];
id <TyphoonResource> configurationProperties = [TyphoonBundleResource withName:@"Configuration.properties"];
[factory attachPostProcessor:[TyphoonPropertyPlaceholderConfigurer configurerWithResource:configurationProperties]];
return factory;
}
。 。如果需要,您可以将修补程序附加到此程序集。
将修补程序附加到默认工厂:
如果要将修补程序应用于默认程序集,则很可能需要再次取消修补。此功能位于待办事项here中。