Objective C - 单元测试私有方法的核心功能? [英] Objective C - Unit testing core functionality in private methods?

查看:83
本文介绍了Objective C - 单元测试私有方法的核心功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过很多情况,我的核心逻辑是私有方法。
您如何进行单元测试,是否存在任何编译时操作以忽略未知/私有方法的编译错误?我知道对于代码的第二部分我可以使用performSelector,但这是一个合理的解决方案吗?

I've been in many situations where my core logic is in private methods. How would you go about unit testing that, is there any kind of compile time manipulation to ignore compile errors for unknown/private methods? I know that for the second part of the code I could use performSelector, but is that a reasonable solution?

例如:

[[self.objectMock expect] privateMethod];
or 
[self.object callPrivateMethodsToExpectSomeOtherBehaviour]

编辑:

这是一个示例,用于说明为什么我需要测试一些私有方法。
这些测试不合理吗?我怎么测试调用clear实际上做了它想做的事情?

Here is an example to demonstrate why I feel like I need to test some private methods. Are these tests not reasonable? How else would I test that calling clear actually does what it suppose to do?

- (void)clear
{
   self.orderNumber = nil;
   [self.items removeAllObjects];
   // Clear the rest of fields
}

- (void)testClearShouldRemoveOrderNumber
{
   Order *order = [[Order alloc] init];
   OCMockObject *orderPartialMock = [OCmockObject partialMockForObject:order];

   [[orderPartialMock.items expect] setOrderNumber:nil];
   [orderPartialMock clear];
   [orderPartialMock verify];
}

- (void)testClearShouldRemoveItems
{
    Order *order = [[Order alloc] init];
    order.items = [[OCMockObject niceMockForClass:[NSMutableArray class]];

    [[orderPartialMock.items expect] removeAllObjects];
    [orderPartialMock performSelector@selector(clear)];
    [orderPartialMock.items verify];
}


推荐答案

方法永远不会私密从某种意义上说,一旦一个类实现了一个方法,它就可以被我的任何人发送。

Methods are never "private" in the sense that once a class implements a method, it can be sent my anyone.

所以,假设你有一个类 Foo 使用私有方法 bar 不在接口声明中。您可以在任何地方仍然调用 bar ,但您可能会获得编译器诊断。

So, let's say you have a class Foo with a "private" method bar that is not in the interface declaration. You could, from anywhere, still invoke bar though you may get a compiler diagnostic.

可能最简单的方法是声明测试使用的类别中的方法。例如:

Probably the simplest approach is to declare the methods in a category that your tests use. For example:

@interface Foo (MyPrivateMethodsUsedForTesting)
- (void)bar;
@end

现在,您可以在没有编译器抱怨的情况下使用它们。注意,这些方法不必在实际的 MyPrivateMethodsUsedForTesting 类别中实现。这种技术有时也被称为非正式协议。

Now, you can use them without the compiler complaining either. Note, the methods do not have to be implemented in an actual MyPrivateMethodsUsedForTesting category. This technique is also sometimes referred to as an "informal protocol."

编辑

另外,正如其他人所说,如果您需要访问私有方法,您可能应该重新审视您的设计。大约30年后这样做,肯定有时候,特别是测试,你需要访问私人的东西,但大多数情况下,它意味着某种类型的设计审查是有序的。

Also, as noted by others, that if you need to access private methods, you probably should revisit your design. After ~30 years doing this, there are definitely times where, especially for tests, you need to access private stuff, but most times it means some type of design review is in order.

这篇关于Objective C - 单元测试私有方法的核心功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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