gpt4 book ai didi

ios - 如何对 NSFetchedResultsControllerDelegate 进行单元测试?

转载 作者:行者123 更新时间:2023-12-01 16:50:44 25 4
gpt4 key购买 nike

我正在尝试为实现 NSFetchedResultsControllerDelegate protocol 的 View Controller 编写单元测试。 .实现的第一个测试(在此 View Controller 的一些其他测试之后)是在插入新对象时验证表行是否插入到 TableView 中。

我的第一个测试实现是:

- (void) setUp {
[super setUp];

sut = [[JODataTableViewController alloc] init];
fetchedResultsCtrlrMock = [OCMockObject niceMockForClass:[NSFetchedResultsController class]];
NSError *__autoreleasing *err = (NSError *__autoreleasing *) [OCMArg anyPointer];
[[[fetchedResultsCtrlrMock expect] andReturnValue:OCMOCK_VALUE((BOOL){YES})] performFetch:err];
[sut setValue:fetchedResultsCtrlrMock forKey:@"fetchedResultsController"];
[sut view]; // This invokes viewDidLoad.
}

- (void) tearDown {
sut = nil;

[super tearDown];
}

- (void) testObjectInsertedInResultsAddsARowToTheTable {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
id tableViewMock = [OCMockObject mockForClass:[UITableView class]];
sut.tableView = tableViewMock;
[[tableViewMock expect] insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationLeft];

[sut controller:nil didChangeObject:nil
atIndexPath:nil
forChangeType:NSFetchedResultsChangeInsert
newIndexPath:indexPath];

[tableViewMock verify];
}

当它试图在 View Controller 中实现功能以移动到绿色状态(TDD)时,我编写了以下代码:
- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}


- (void) controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableViewCell *cell;

switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:@[newIndexPath]
withRowAnimation:UITableViewRowAnimationLeft];
break;

}
}


- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}

但是,我无法让它通过,错误是:
Test Case '-[JODataTableViewControllerTests testObjectInsertedInResultsAddsARowToTheTable]' started.
Unknown.m:0: error: -[JODataTableViewControllerTests testObjectInsertedInResultsAddsARowToTheTable] : OCMockObject[UITableView]: unexpected method invoked: isKindOfClass:<??>
Test Case '-[JODataTableViewControllerTests testObjectInsertedInResultsAddsARowToTheTable]' failed (0.001 seconds).

我尝试在测试的准备部分添加一次或多次以下行,结果相同。
[[[tableViewMock expect] andReturnValue:OCMOCK_VALUE((BOOL){YES})] isKindOfClass:[OCMArg any]];

如您所见,我目前正在使用 OCUnitOCMock .仅当无法使用此工具集创建此类测试时,我才会考虑其他工具,在这种情况下,如果存在它们的局限性,我将不胜感激。

据我了解,即使被告知,模拟也无法对其类(class)的性质“撒谎”。此外,该错误未提供有关 UITableView 类的信息。在寻找。我知道使用 -isKindOfClass: 进行测试不是一个好习惯,但这不是我的代码。

感谢您的帮助。

最佳答案

解决模拟隐藏了内部行为的对象的一种方法是使用部分模拟。在你的情况下:

id tableViewMock = [OCMock partialMockForObject:[[UITableView alloc] init]];

当我这样做时,我通常会稍微更改名称并称之为 tableViewPartial

关于ios - 如何对 NSFetchedResultsControllerDelegate 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com