ios - 用 Typhoon 注入(inject)模拟

标签 ios unit-testing typhoon

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

这是我的ViewController中的代码:

   - (instancetype)init {
    self = [super init];

    MDMainAssembly *assembly = (MDMainAssembly *) [TyphoonComponentFactory defaultFactory];
    self.alertManager = [assembly alertManager];

    return self;
   }

以下是我尝试更改注入(inject)的方法:

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

如何摆脱这种情况?

最佳答案

我们为有限数量的情况创建了 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;
}

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

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

如果您要将修补程序应用于默认程序集,您很可能想再次取消修补。此功能已积压 here

关于ios - 用 Typhoon 注入(inject)模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21906451/

相关文章:

ios - Typhoon:从程序集中检索 Storyboard时出现异常

ios - UIBezierPath bezierPathWithArcCenter 未正确居中

unit-testing - 浅层渲染 Jest 快照

objective-c - Swift 2 - @objc 抛出错误的协议(protocol)

python - Web 抓取的测试驱动开发 (TDD)

visual-studio-2010 - 无法使用 Visual Studio 调试 NUnit 测试

ios - 如何访问已激活 plist 的程序集?

jquery - 允许 ics 在 iOS 的 phonegap 应用程序中打开

ios - 不能在 swift 中使用 "Analyze"吗?

ios - iAd 开发 - 错误多于成功的测试广告