gpt4 book ai didi

ios - OCMock "expected method was not invoked"即使我自己调用它

转载 作者:可可西里 更新时间:2023-11-01 04:22:36 27 4
gpt4 key购买 nike

我正在尝试在 iOS 项目中设置一个简单的 OCMock 单元测试,以熟悉框架。

我有一个模拟的 DataLoader 类,即使我自己调用该方法,我的期望也失败了:

- (void)testSimpleMocking {
// Mock the class
id mock = [OCMockObject niceMockForClass:[DataLoader class]];

// Override the 'dispatchLoadToAppDelegate:' to be a no-op
[[[mock stub] andReturn:nil] dispatchLoadToAppDelegate:[OCMArg any]];

// Expect the method to be called
[[mock expect] dispatchLoadToAppDelegate:[OCMArg any]];

// Call the method
[mock dispatchLoadToAppDelegate:nil];

// Verify
[mock verify];
}

但是,当我运行这个测试时,我收到错误:

/Users/Craig/projects/MyApp/Unknown.m: -[MockingDataLoaderTest testSimpleMocking] : OCMockObject[DataLoader]:
expected method was not invoked: dispatchLoadToAppDelegate:<OCMAnyConstraint: 0x1a3d890>

当我自己调用方法时,这怎么可能?

编辑:一个更复杂的案例:

- (void)testDataLoaderWaitsForDownload {
id mock = [OCMockObject niceMockForClass:[DataLoader class]];
id metadataItem = [OCMockObject niceMockForClass:[NSMetadataItem class]];

// Prepare NSMetadataItem
[[[metadataItem expect] andReturn:nil] valueForAttribute:NSMetadataItemURLKey];

// CODERUN
[mock waitForDownload:metadataItem thenLoad:YES];

//VERIFY
[metadataItem verify];
}

以及 waitForDownload:thenLoad: 方法的实现:

- (void)waitForDownload:(NSMetadataItem *)file thenLoad:(BOOL)load {
NSURL *metadataItemURL = [file valueForAttribute:NSMetadataItemURLKey];
...

失败并出现错误:

Unknown.m:0: error: -[MockingDataLoaderTest testDataLoaderWaitsForDownload] : OCMockObject[NSMetadataItem]: expected method was not invoked: valueForAttribute:@"kMDItemURL"

最佳答案

在您的测试中,stub 优先,因为它首先被调用。如果您调换 expectstub 的顺序,您的测试应该会通过。

同时使用 expectstub 的原因(具有相同的参数 expectations)是为了确保至少发生一次调用,然后响应后续的调用调用没有失败。

如果您真的只想调用一个方法,只需将 andReturn: 添加到 expect 子句中...

- (void)test_dispatchLoadToAppDelegate_isCalledExactlyOnce {
// Mock the class
id mock = [OCMockObject niceMockForClass:[DataLoader class]];

// Expect the method to be called
[[[mock expect] andReturn:nil] dispatchLoadToAppDelegate:[OCMArg any]];

// Call the method
[mock dispatchLoadToAppDelegate:nil];

// Verify
[mock verify];
}

另一种情况:

- (void)test_dispatchLoadToAppDelegate_isCalledAtLeastOnce {
// Mock the class
id mock = [OCMockObject niceMockForClass:[DataLoader class]];

// Expect the method to be called
[[[mock expect] andReturn:nil] dispatchLoadToAppDelegate:[OCMArg any]];

// Handle subsequent calls
[[[mock stub] andReturn:nil] dispatchLoadToAppDelegate:[OCMArg any]];

// Call the method
[mock dispatchLoadToAppDelegate:nil];

// Call the method (again for fun!)
[mock dispatchLoadToAppDelegate:nil];

// Verify
[mock verify];
}

对于这种特殊情况,看起来您可以使用 niceMockForClass 但如果您希望 stub 返回一个非零值,那么您必须调用 stub无论哪种方式。

关于ios - OCMock "expected method was not invoked"即使我自己调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17297618/

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