2014-02-20 51 views
2

我在写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]; 

如何摆脱这种情况?

回答

2

我们为有限数量的案例创建了defaultFactory特性。主要是:

  • 访问台风并查找不受台风管理的类的依赖关系。通常这不是必需的。

尽管您可以在测试中使用它,但我们建议您为每次测试运行创建并销毁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

+0

但我的视图控制器将如何访问配置的工厂或我应该也将控制器实例化到工厂? –

+0

是的,我们建议使用Typhoon构建视图控制器。然后,注入**当前用例**的依赖关系。但是对于下一个用例,请注入程序集:http://bit.ly/1nQce7k,然后查找下一个对象图。或使用:http://bit.ly/1a93dFE或:http://bit.ly/1mdnpZ1。 。如果这不适合你,你想坚持defaultAssembly,或许一个更简单的方法来模拟是提供一个setter,所以你可以注入一个模拟工厂。 。这取决于您是否需要配置的集成测试或纯粹的单元测试。 –

+0

您是否尝试过示例应用程序? https://github.com/typhoon-framework/Typhoon-example –