台风:如何使一个符合生产协议的实例与另一个要进行测试的实例相符? [英] Typhoon: How to get an instance conforming to a protocol for production, and another for tests?

查看:119
本文介绍了台风:如何使一个符合生产协议的实例与另一个要进行测试的实例相符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在台风中定义了一个ApplicationAssembly.

I've defined an ApplicationAssembly in Typhoon.

所以我想说的是:这个类X需要注入符合Foo协议的东西.这是RealFoo,这是TestFoo.当我在现实生活中运行X时,我想要以获得RealFoo,但是当我运行集成测试时,我希望它获得TestFoo".

So what I want to do is say: "This class X needs to be injected with something conforming to the Foo protocol. This is a RealFoo, this is a TestFoo. When I'm running X in real life, I want it to get a RealFoo, but when I'm running my integration tests, I want it to get a TestFoo".

我该怎么做?

推荐答案

有几种推荐的方法:

使用台风修补程序

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"];


将与环境有关的组件组合在一起

另一种方法是将依赖于环境的组件组合在一起.如果您使用的是XML样式程序集,则可以为生产和测试方案加载不同的文件集,包括基础程序集和任何与环境有关的文件.

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.


使用TyphoonConfig

另一种方法是使用TyphoonConfig.有关此功能的详细信息,请此处.

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


上面的示例适用于台风2.0.这在Typhoon 3.0上仍然可以正常使用,但是在程序集激活方面有些整洁:

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];

  • 在Typhoon 3.0中,仅当协作程序集受协议(不是具体类型)支持或希望覆盖其中一个程序集时,才需要声明协作程序集.
  • 您可以使用例如[assembly.colloaboratingAssembly stormTrooper]
  • 从协作程序集中解析零部件

    • 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]
    • 这篇关于台风:如何使一个符合生产协议的实例与另一个要进行测试的实例相符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆