2

我正在尝试使用 Typhoon 编写 XCTest 并注入模拟依赖项。

这是我的代码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];

但是测试失败,因为无法将此工厂设置为默认值。我在AppDelegate工厂配置:

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

如何摆脱这种局面?

4

1 回答 1

2

我们为有限数量的情况创建了 defaultFactory 功能。主要是:

  • 访问 Typhoon 并查找不受 Typhoon 管理的类的依赖项。通常这不是必需的。

尽管您可以在测试中使用它,但我们建议您为每次测试运行创建和销毁 Typhoon 容器。为避免重复,您可以创建如下方法:

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

. . 如果需要,您可以将修补程序附加到此程序集。

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

如果您要将修补程序应用于默认程序集,您很可能希望再次取消修补。此功能在这里积压。

于 2014-02-20T12:18:53.360 回答