stub [[SomeClazz alloc] init]不起作用但接受的答案说它应该可以工作 [英] stub [[SomeClazz alloc] init] not work but accepted answer says it should work

查看:95
本文介绍了stub [[SomeClazz alloc] init]不起作用但接受的答案说它应该可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试功能非常简单:

My function under test is very simple:

@implementation MyHandler
...
-(void) processData {
  DataService *service = [[DataService alloc] init];
  NSDictionary *data = [service getData];
  [self handleData:data];
}

@end

我用 OCMock 3 对其进行单元测试。

I use OCMock 3 to unit test it.

我需要存根 [[DataService alloc] init] 才能返回模拟实例,我尝试了从这个问题回答(这是一个可接受的答案)到存根 [[SomeClazz alloc] init]

I need to stub the [[DataService alloc] init] to return a mocked instance, I tried the answer from this question (which is an accepted answer) to stub [[SomeClazz alloc] init]:

// Stub 'alloc init' to return mocked DataService instance,
// exactly the same way as the accepted answer told
id DataServiceMock = OCMClassMock([DataService class]);
OCMStub([DataServiceMock alloc]).andReturn(DataServiceMock);
OCMStub([DataServiceMock init]).andReturn(DataServiceMock);

// run function under test
[MyHandlerPartialMock processData];

// verify [service getData] is invoked
OCMVerify([dataServiceMock getData]);

我在测试功能中设置了断点,我肯定 [service运行单元测试时调用getData] ,但我的上述测试代码(OCMVerify)失败。为什么?

I have set break point in function under test, I am sure [service getData] is called when run unit test, but my above test code (OCMVerify) fails. Why?

是否因为被测功能未使用我的模拟 DataService ?但是在这个问题中接受的答案告诉它应该有效。我现在感到困惑......

Is it because the function under test is not using my mocked DataService? But the answer accepted in that question tells it should work. I get confused now...

我想知道如何将 [[SomeClazz alloc] init] 存根到使用OCMock返回模拟实例?

I want to know how to stub [[SomeClazz alloc] init] to return mocked instance with OCMock?

推荐答案

您无法模拟 init ,因为它由mock对象本身实现。模拟 init 的原因在于您链接的答案,因为它是自定义初始化方法。如果您不想使用依赖注入,则必须为 DataService 编写自定义 init 方法,您可以模拟。

You cannot mock init as it is implemented by the mock object itself. The reason that mocking init works in the answer you linked is because it is a custom init method. If you do not want to use dependency injection, you will have to write a custom init method for DataService that you can mock.

在您的实现中添加自定义 init 方法:

In your implementation add a custom init method:

// DataService.m
...
- (id) initForTest
{
    self = [super init];
    if (self) {
        // custom initialization here if necessary, otherwise leave blank
    }

    return self;
}
...

然后更新 MyHandler 实现调用此 initForTest

@implementation MyHandler
...
-(void) processData {
  DataService *service = [[DataService alloc] initForTest];
  NSDictionary *data = [service getData];
  [self handleData:data];
}

@end

最后将你的测试更新为stub initForTest

And finally update your test to stub initForTest:

id DataServiceMock = OCMClassMock([DataService class]);
OCMStub([DataServiceMock alloc]).andReturn(DataServiceMock);
OCMStub([DataServiceMock initForTest]).andReturn(DataServiceMock);

// run function under test
[MyHandlerPartialMock processData];

// verify [service getData] is invoked
OCMVerify([dataServiceMock getData]);

随意重命名 initForTest 只要它不被称为 init

Feel free to rename initForTest, so long as it isn't called init.

这篇关于stub [[SomeClazz alloc] init]不起作用但接受的答案说它应该可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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