TyphoonPatcher,用于单元测试中的模拟 [英] TyphoonPatcher for mocking in unit tests

查看:105
本文介绍了TyphoonPatcher,用于单元测试中的模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Assembly:

@interface MDUIAssembly : TyphoonAssembly

@property (nonatomic, strong, readonly) MDServiceAssembly *services;
@property (nonatomic, strong, readonly) MDModelAssembly *models;

- (id)choiceController;

@end

@implementation MDUIAssembly

- (void)resolveCollaboratingAssemblies
{
    _services = [TyphoonCollaboratingAssemblyProxy proxy];
    _models = [TyphoonCollaboratingAssemblyProxy proxy];
}

- (id)choiceController
{
    return [TyphoonDefinition withClass:[MDChoiceViewController class]
                          configuration: ^(TyphoonDefinition *definition) {
        [definition useInitializer:@selector(initWithAnalytics:diary:)
                        parameters: ^(TyphoonMethod *initializer) {
            [initializer injectParameterWith:[_services analytics]];
            [initializer injectParameterWith:[_models diary]];
        }];
    }];
}

@end

这是我要在测试中尝试做的事情:

Here what I'm trying to do in tests:

- (void)setUp
{
    patcher = [TyphoonPatcher new];
    MDUIAssembly *ui = (id) [TyphoonComponentFactory defaultFactory];
    [patcher patchDefinition:[ui choiceController] withObject:^id{
       return mock([MDChoiceViewController class]);
    }];
    [[TyphoonComponentFactory defaultFactory] attachPostProcessor:patcher];
}

- (void) tearDown 
{
   [super tearDown];
   [patcher rollback];
}

不幸的是,我的setUp失败并显示了下一条消息:

Unfortunately my setUp fails with next message:

-[MDChoiceViewController key]: unrecognized selector sent to instance 0xbb8aaf0

我做错了什么?

推荐答案

此处还有一些其他建议,可与 单元测试与集成测试:

在台风中,我们遵循传统术语:

In Typhoon we adhere to the traditional terms:

  • 单元测试:与协作者隔离地测试您的班级.在这里,您可以插入诸如模拟或存根之类的测试双打来代替所有实际依赖项.

  • Unit Tests : Testing your class in isolation from collaborators. This is where you inject test doubles like mocks or stubs in place of all of the real dependencies.

集成测试:使用真正的协作者测试您的班级.尽管您可能会修补我们的组件以使系统处于该测试所需的状态.

Integration Tests: Testing your class using real collaborators. Although you may patch our a component in order to put the system in the required state for that test.

因此,任何使用TyphoonPatcher的测试都可能是集成测试.

So any test that uses TyphoonPatcher would probably be an integration test.

此处的更多信息:台风集成测试

解决协作程序集:

此功能在早期版本的Typhoon中是必需的,但不再需要. TyphoonAssembly的子类的任何属性都将被视为协作程序集.删除以下内容:

This was required in earlier version of Typhoon, but is not longer needed. Any properties that are are sub-class of TyphoonAssembly will be treated as collaborating assemblies. Remove the following:

- (void)resolveCollaboratingAssemblies
{
    _services = [TyphoonCollaboratingAssemblyProxy proxy];
    _models = [TyphoonCollaboratingAssemblyProxy proxy];
}

测试实例化其自己的程序集:

我们建议测试实例化并在TyphoonComponentFactory上拆除它们.优点是:

We recommend that tests instantiate and tear down their on TyphoonComponentFactory. The advantages are:

  • [TyphoonComponentFactory defaultFactory]是全局的,并且有一些缺点.
  • 集成测试可以定义自己的补丁程序,而不必担心将系统恢复到原始状态.
  • 除了使用TyphoonPatcher之外,如果您愿意,还可以创建一个程序集,其中某些零部件的定义将被覆盖.
  • [TyphoonComponentFactory defaultFactory] is a global and has some drawbacks.
  • Integration tests can define their own patches without having to worry about putting the system back in the original state.
  • In addition to using TyphoonPatcher, if you wish you can create an assembly where the definitions for some components are overridden.

这篇关于TyphoonPatcher,用于单元测试中的模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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