Typhoon Injection Layout

I am trying to write XCTest and introduce a mocked dependency with Typhoon.

Here is the code in my ViewController :

  - (instancetype)init { self = [super init]; MDMainAssembly *assembly = (MDMainAssembly *) [TyphoonComponentFactory defaultFactory]; self.alertManager = [assembly alertManager]; return self; } 

This is how I try to change the 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]; 

However, the tests fail because this factory cannot be set as the default. I configure in the AppDelegate factory:

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

How to get out of this situation?

+2
ios unit-testing typhoon
source share
1 answer

We created a defaultFactory function for a limited number of cases. The main thing:

  • Gaining access to Typhoon and finding dependencies for a class that is not managed by Typhoon. This is usually not required.

Although you can use it in tests, we recommend that you instead create and destroy a Typhoon container for each test run. To avoid duplication, you can 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 necessary, you can attach a patcher to this assembly.

Attaching a patcher to factory default:

If you want to apply the patcher to the default assembly, most likely you will want to remove the patch again. This feature is in the backlog here .

+2
source share

All Articles