我遇到过很多情况,我的核心逻辑在私有方法中。您将如何进行单元测试,是否有任何类型的编译时操作来忽略未知/私有方法的编译错误?我知道对于代码的第二部分,我可以使用 performSelector,但这是一个合理的解决方案吗?
例如:
[[self.objectMock expect] privateMethod];
or
[self.object callPrivateMethodsToExpectSomeOtherBehaviour]
编辑:
这是一个示例来说明为什么我觉得我需要测试一些私有方法。这些测试不合理吗?否则我将如何测试调用 clear 是否真正做到了它应该做的事情?
- (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];
}