在 Objective C 中执行 BDD 时,模拟协作者的最好方法是什么?

发布于 2025-01-06 12:02:44 字数 2666 浏览 0 评论 0原文

我一直在使用 Objective C 和 Cocoa/iOS 并像模拟者一样进行测试。 (定义

  • 我想使用以下方式模拟对象的协作者OCMock。
  • 据我所知,在 Objective C 中有两种方法可以做到这一点:

    1. 依赖注入
    2. 设置内部状态 - 通过访问器或 setValue:forKey:

我应该使用哪个?

我不知道就像其中任何一个。但我必须使用一个......除非还有其他我不知道的选择。

1.依赖注入

这会让我的代码变得混乱,尤其是当 SUT 有 2/3 的协作者时。如果 SUT 需要传递 1/2 参数,事情就会开始看起来确实非常混乱。

我知道除了 3 个依赖项之外,还有太多的依赖项,并且该对象应该分成其他部分......但即使有 2 个依赖项和 1 个参数,它仍然丑陋如罪。

2.设置内部状态

这会扰乱类的内部结构——我认为这在测试中是一个很大的禁忌。

访问器肯定已经过时了——它们暴露了没有人应该知道的数据。 我可以使用 setValue:forKey: ...但这感觉像是一个可怕的黑客。

这也意味着我必须初始化 SUT,然后将真正的协作者替换为模拟协作者,然后运行被测试的方法,这感觉很混乱。

我的问题

在进行 BDD 时,在 Objective C 中模拟协作者的最好方法是什么?

代码

使用 setValue:forKey 进行模拟

@interface JGCompositeCommand : JGCommand <JGCompositeCommandProtocol> {
    NSMutableArray *commands;
    JGCommandFinderFactory *commandFinderFactory;
}

-(id <JGCommandProtocol>)initWithName:(NSString *)name_ recoverer:(id <JGCommandRecoveryProtocol>)recoverer_ executor:(id <JGCommandExecutorProtocol>)executor_;

@end

@implementation JGCompositeCommand

-(id)initWithName:(NSString *)name_ recoverer:(id)recoverer_ {
    self = [super initWithName:name_ recoverer:recoverer_];
    if (self) {
      commands = [NSMutableArray array];
      commandFinderFactory = [[JGCommandFinderFactory alloc] init];
    }
    return self;
}

-(id <JGCommandProtocol>)commandWithName:(NSString *)name_ {
    return [[commandFinderFactory commandFinderWithCommandName:name_ andCommands:commands] findCommandWithName];
}

@end

@interface JGCommandTestCase : SenTestCase {
    JGCompositeCommand *compositeCommand;
    OCMockObject *commandFinderFactoryMock;
}

@end 

@implementation JGCommandTestCase

-(void)setUp {
    [super setUp];
    compositeCommand = [[JGCompositeCommand alloc] initWithName:@"" recoverer:nil];
    commandFinderFactoryMock = [OCMockObject mockForClass:[JGCommandFinderFactory class]];
    // Hack alert! Ugh.
    [compositeCommand setValue:commandFinderFactoryMock forKey:@"commandFinderFactory"];
}

-(void)testGivenCommandNotFoundShouldThrow {
    // ** Setup **
    [[[commandFinderFactoryMock expect] andReturn:...] commandFinderWithCommandName:... andCommands:...];

    // ** Execute **
    [compositeCommand commandWithName:@"Blah"];

    // ** Asserts **
    [commandFinderFactoryMock verify];
}

@end 

I've been working with Objective C and Cocoa/iOS and testing like a mockist. (Definition)

  • I want to mock out the collaborators of an object using OCMock.
  • There are two ways of doing this in Objective C I'm aware of:

    1. Dependency Injection
    2. Setting internal state - either through Accessors or setValue:forKey:

Which Should I Use?

I don't like either of these. But I've got to use one... unless there's other options I'm not aware of.

1. Dependency Injection

This clutters my code, especially when the SUT has 2/3 collaborators. If the SUT needs to be passed 1/2 parameters, things start to look very messy indeed.

I understand that much beyond 3 there are too many dependencies and the object should be split up into other parts... but even with 2 dependencies and 1 parameter, it's still ugly as sin.

2. Setting Internal State

This messes with the internals of a class - which I thought was a big no-no in testing.

Accessors are definitely out - they expose data that no-one should know about.
I can use setValue:forKey: ... but this feels like a horrible hack.

It also means I have to init the SUT then swap out the real collaborators for mock ones, then run the method under test, which feels messy.

My Question

What's the nicest way to mock out collaborators in Objective C when doing BDD?

Code

Mocking using setValue:forKey:

@interface JGCompositeCommand : JGCommand <JGCompositeCommandProtocol> {
    NSMutableArray *commands;
    JGCommandFinderFactory *commandFinderFactory;
}

-(id <JGCommandProtocol>)initWithName:(NSString *)name_ recoverer:(id <JGCommandRecoveryProtocol>)recoverer_ executor:(id <JGCommandExecutorProtocol>)executor_;

@end

@implementation JGCompositeCommand

-(id)initWithName:(NSString *)name_ recoverer:(id)recoverer_ {
    self = [super initWithName:name_ recoverer:recoverer_];
    if (self) {
      commands = [NSMutableArray array];
      commandFinderFactory = [[JGCommandFinderFactory alloc] init];
    }
    return self;
}

-(id <JGCommandProtocol>)commandWithName:(NSString *)name_ {
    return [[commandFinderFactory commandFinderWithCommandName:name_ andCommands:commands] findCommandWithName];
}

@end

@interface JGCommandTestCase : SenTestCase {
    JGCompositeCommand *compositeCommand;
    OCMockObject *commandFinderFactoryMock;
}

@end 

@implementation JGCommandTestCase

-(void)setUp {
    [super setUp];
    compositeCommand = [[JGCompositeCommand alloc] initWithName:@"" recoverer:nil];
    commandFinderFactoryMock = [OCMockObject mockForClass:[JGCommandFinderFactory class]];
    // Hack alert! Ugh.
    [compositeCommand setValue:commandFinderFactoryMock forKey:@"commandFinderFactory"];
}

-(void)testGivenCommandNotFoundShouldThrow {
    // ** Setup **
    [[[commandFinderFactoryMock expect] andReturn:...] commandFinderWithCommandName:... andCommands:...];

    // ** Execute **
    [compositeCommand commandWithName:@"Blah"];

    // ** Asserts **
    [commandFinderFactoryMock verify];
}

@end 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潜移默化 2025-01-13 12:02:44

我们采用的方法是使协作者成为单例,并提供一种在测试时注入模拟实例的方法:

static JGCommandFinder *sharedFinder = nil;

+(JGCommandFinder *)sharedFinder {
    if (sharedFinder == nil) sharedFinder = [[JGCommandFinder alloc] init];
    return sharedFinder;
}

+(void)setSharedFinder:(JGCommandFinder *)instance {
    sharedFinder = instance;
}

它非常灵活,因为您可以使用实际对象,即使真实对象已经初始化也可以注入模拟,并通过将实例设置为零来将其重置为使用真实对象。

The approach we've landed on is to make the collaborators singletons, and provide a way to inject a mock instance at test time:

static JGCommandFinder *sharedFinder = nil;

+(JGCommandFinder *)sharedFinder {
    if (sharedFinder == nil) sharedFinder = [[JGCommandFinder alloc] init];
    return sharedFinder;
}

+(void)setSharedFinder:(JGCommandFinder *)instance {
    sharedFinder = instance;
}

It's very flexible, as you can use the actual object, inject a mock even if the real one has been initialized, and reset it to use the real object by setting the instance to nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文