用台风注入模拟 [英] Injecting mock with Typhoon
问题描述
我正在尝试编写XCTest并使用Typhoon注入模拟的依赖项.
I'm trying to write XCTest and inject mocked dependency with Typhoon.
这是我的ViewController
中的代码:
- (instancetype)init {
self = [super init];
MDMainAssembly *assembly = (MDMainAssembly *) [TyphoonComponentFactory defaultFactory];
self.alertManager = [assembly alertManager];
return self;
}
这是我要更改注射方式的方法:
Here is how I'm trying to change injection:
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
工厂中配置:
However tests are failing because this factory not possible to set as default. I configure in AppDelegate
factory:
TyphoonComponentFactory *factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
[MDMainAssembly assembly],
]];
[factory makeDefault];
如何摆脱这种情况?
推荐答案
我们为有限的情况创建了defaultFactory功能.主要是:
We created the defaultFactory feature for a limited number of cases. The main is:
- 访问Typhoon并查找非Typhoon管理的类的依赖关系.通常,这不是必需的.
尽管您可以在测试中使用它,但我们建议您为每次测试运行创建并销毁Typhoon容器.为避免重复,您可以创建如下方法:
Although you could use it in tests, we recommend instead creating and destroying a Typhoon container for each test run. To avoid duplication, you could create a method as follows:
@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;
}
. .如果需要,您可以将修补器连接到此装配件.
. . if required you could attach a patcher to this assembly.
将修补程序附加到默认工厂:
如果要将修补程序应用于默认程序集,则很可能要再次取消修补程序.此功能位于积压的此处.
If you were to apply a patcher to the default assembly, you'd most probably want to un-patch again. This feature is in the backlog here.
这篇关于用台风注入模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!