用台风注入模拟

我正在写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]; 

然而,testing失败,因为这个工厂不可能设置为默认。 我在AppDelegate工厂中configuration:

  TyphoonComponentFactory *factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[ [MDMainAssembly assembly], ]]; [factory makeDefault]; 

如何摆脱这种情况?

我们为有限的几个案例创build了defaultFactory特性。 主要是:

  • 进入台风,并寻找一个不受台风pipe理的class级的依赖。 通常这不是必需的。

尽pipe您可以在testing中使用它,但我们build议您在每次testing中创build并销毁一个Typhoon容器。 为了避免重复,您可以创build一个方法如下:

 @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; } 

。 。 如果需要,你可以附加一个修补程序到这个程序集。

将修补程序附加到默认工厂:

如果您要将补丁程序应用于默认程序集,则很可能需要重新进行修补。 这个function在这里是积压的。