Typhoon: How to get an instance conforming to a protocol for production, and another for tests?

旧街凉风 提交于 2019-12-05 13:58:33

There are several recommended ways to do this:

Use the Typhoon Patcher

Typhoon-patcher allows loading a base assembly, but with one or more components patched out with another definition, or a given object instance. Here's an example of patching out a component with a mock:

MiddleAgesAssembly* assembly = [MiddleAgesAssembly assembly];
TyphoonComponentFactory* factory = [TyphoonBlockComponentFactory factoryWithAssembly:assembly];

TyphoonPatcher* patcher = [[TyphoonPatcher alloc] init];
[patcher patchDefinition:[assembly knight] withObject:^id
{
    Knight* mockKnight = mock([Knight class]);
    [given([mockKnight favoriteDamsels]) willReturn:@[
        @"Mary",
        @"Janezzz"
    ]];

    return mockKnight;
}];

[factory attachPostProcessor:patcher];

Knight* knight = [factory componentForKey:@"knight"];


Group Environment Dependent Components Together

Another approach is to group environment dependent components together. If you're using the XML style assembly, you can load a different set of files for production vs test scenarios, including the base assembly and any environment dependent files.

The same thing can be achieved in the block-based assembly, as follows:

TyphoonComponentFactory* factory = [[TyphoonBlockComponentFactory alloc] initWithAssemblies:@[
    [MiddleAgesAssembly assembly],
    [StarWarsAssembly assembly]
]];

Knight* cavalryMan = [(MiddleAgesAssembly*) factory cavalryMan];
Knight* stormTrooper = [(StarWarsAssembly*) factory stormTrooper];

For more information consult Modularization of Assemblies in the Typhoon documentation, or check out the sample app, which contains an example of this.


Use a TyphoonConfig

Another approach is to use TyphoonConfig. Details for this feature are here.


Edit:

The above example is for Typhoon 2.0. This still works fine with Typhoon 3.0, but somewhat neater is assembly activation:

MiddleAgesAssembly *assembly = [[MiddleAgesAssembly new] activate]; 
Knight *knight = [assembly knight];
  • In Typhoon 3.0 you only need to declare collaborating assemblies if they are backed by a protocol not a concrete type, or if you wish to override one of your assemblies.
  • You can resolve components from the collaborating assemblies with eg[assembly.colloaboratingAssembly stormTrooper]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!