Ios 在Xcode 9 GM中运行单元测试时出现神秘的NSInternalInconsistencyException异常

Ios 在Xcode 9 GM中运行单元测试时出现神秘的NSInternalInconsistencyException异常,ios,xcode,xctest,xcode9-beta,xcode9,Ios,Xcode,Xctest,Xcode9 Beta,Xcode9,我正在Xcode 9 GM上运行我的iOS应用程序的单元测试,其中一些测试失败,出现了一个奇怪的NSInternalInconsistencyException,抱怨无法报告某些测试断言,因为涉及的测试没有关联的XCTestRun对象。我使用OCMockito+OCHamcrest进行模拟和调用验证 出于演示目的,假设我的应用程序名为MyTestApp,我有一个测试类FooTest(继承自XCTestCase)。在-setUp中,我为测试创建并连接各种模拟对象,在-tearDown中,我将它们设

我正在Xcode 9 GM上运行我的iOS应用程序的单元测试,其中一些测试失败,出现了一个奇怪的NSInternalInconsistencyException,抱怨无法报告某些测试断言,因为涉及的测试没有关联的XCTestRun对象。我使用OCMockito+OCHamcrest进行模拟和调用验证

出于演示目的,假设我的应用程序名为MyTestApp,我有一个测试类FooTest(继承自XCTestCase)。在-setUp中,我为测试创建并连接各种模拟对象,在-tearDown中,我将它们设置为nil以防万一

下面是我得到的一个例外的例子:

2017-09-19 13:23:01.852729-0700 xctest[17006:5392130] *** Assertion failure in -[FooTest recordFailureWithDescription:inFile:atLine:expected:], /Library/Caches/com.apple.xbs/Sources/XCTest_Sim/XCTest-13201/Sources/XCTestFramework/Core/XCTestCase.m:308 /Users/fooUser/Workspaces/MyTestApp/tests/FooTest.mm:46: error: -[FooTest testSomething] : failed: caught "NSInternalInconsistencyException", "Unable to report test assertion failure 'Expected 1 matching invocation, but received 0' from /Users/fooUser/Workspaces/MyTestApp/tests/FooTest.mm:135 because it was raised inside test case -[FooTest testSomethingElse] which has no associated XCTestRun object. This may happen when test cases are constructed and invoked independently of standard XCTest infrastructure." ( 0 CoreFoundation 0x000000010255d1cb __exceptionPreprocess + 171 1 libobjc.A.dylib 0x0000000101ebff41 objc_exception_throw + 48 2 CoreFoundation 0x0000000102562362 +[NSException raise:format:arguments:] + 98 3 Foundation 0x0000000101827089 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193 4 XCTest 0x0000000101d96875 -[XCTestCase recordFailureWithDescription:inFile:atLine:expected:] + 518 5 MyAppUnitTests 0x000000011d7f2f5a -[HCXCTestFailureReporter executeHandlingOfFailure:] + 154 6 MyAppUnitTests 0x000000011d7f2b73 -[HCTestFailureReporter handleFailure:] + 67 7 MyAppUnitTests 0x000000011d64ec9a MKTFailTest + 217 8 MyAppUnitTests 0x000000011d64c3f4 -[MKTExactTimes verifyData:] + 254 9 MyAppUnitTests 0x000000011d64f2ba -[MKTBaseMockObject verifyInvocation:usingVerificationMode:] + 137 10 MyAppUnitTests 0x000000011d64f20b -[MKTBaseMockObject handlingVerifyOfInvocation:] + 115 11 MyAppUnitTests 0x000000011d64f15a -[MKTBaseMockObject forwardInvocation:] + 64 12 CoreFoundation 0x00000001024dfed8 ___forwarding___ + 760 13 CoreFoundation 0x00000001024dfb58 _CF_forwarding_prep_0 + 120 14 MyAppUnitTests 0x000000011c40b486 -[FooTest setUp] + 294 15 XCTest 0x0000000101d97b39 __24-[XCTestCase invokeTest]_block_invoke_3 + 31 16 XCTest 0x0000000101d97809 __24-[XCTestCase invokeTest]_block_invoke + 271 17 XCTest 0x0000000101ddff45 -[XCUITestContext performInScope:] + 183 18 XCTest 0x0000000101d976ef -[XCTestCase invokeTest] + 141 2017-09-19 13:23:01.852729-0700 xctest[17006:5392130]***在[FooTest recordFailureWithDescription:infle:atLine:expected:],/Library/Caches/com.apple.xbs/Sources/xctest_Sim/xctest-13201/Sources/XCTestFramework/Core/xctcase.m:308中断言失败 /Users/fooUser/workspace/MyTestApp/tests/FooTest.mm:46:错误:-[FooTest testSomething]:失败:捕获到“nsinternalinconsistenceexception”,“无法报告测试断言失败”预期有1个匹配调用,但从/Users/foouuser/workspace/MyTestApp/tests/FooTest.mm:135收到0,因为它是在测试用例中引发的-[FooTest testSomethingElse]没有关联的XCTestRun对象。当独立于标准XCTest基础架构构建和调用测试用例时,可能会发生这种情况。” ( 0 CoreFoundation 0x000000010255d1cb例外预处理+171 1 libobjc.A.dylib 0x0000000101ebff41 objc_异常_抛出+48 2 CoreFoundation 0x0000000102562362+[N异常提升:格式:参数:][98 3基金会0x000 000 0101827 089- [ NSRealStutsHuffer-HuffLeuluIn方法:对象:文件:文件号:描述:] + 193 4 XCTest 0x0000000101d96875-[XCTESCASE recordFailureWithDescription:infle:atLine:expected:+518 5 MyAppUnitTests 0x000000011d7f2f5a-[HCXCTestFailureReporter执行故障处理:+154 6 MyAppUnitTests 0x000000011d7f2b73-[HCTestFailureReporter handleFailure:+67 7 MyAppUnitTests 0x000000011d64ec9a MKTFailTest+217 8 MyAppUnitTests 0x000000011d64c3f4-[MKTExactTimes验证数据:][254 9 MyAppUnitTests 0x000000011d64f2ba-[MKTBaseMockObject验证调用:usingVerificationMode:+137 10 MyAppUnitTests 0x000000011d64f20b-[MKTBaseMockObject HandlingVerifyOfication:+115 11 MyAppUnitTests 0x000000011d64f15a-[MKTBaseMockObject forwardInvocation:+64 12 CoreFoundation 0x00000001024dfed8\uuuuuuuuuuuuuuuu+760 13 CoreFoundation 0x00000001024dfb58\u CF\u转发\u准备\u 0+120 14 MyAppUnitTests 0x000000011c40b486-[FooTest setUp]+294 15 XCTest 0x0000000101d97b39\uuU 24-[XCTestCase invokeTest]\u块\u invoke\u 3+31 16 XCTest 0x0000000101d97809_uu24-[XCTESCASE invokeTest]u块_uinvoke+271 17 XCTest 0x0000000101ddff45-[XcutestContext性能范围:][183 18 XCTest 0x0000000101d976ef-[XCTestCase invokeTest]+141 有几点值得注意:

  • 单元测试不会初始化XCTESCASE或XCTest框架之外的任何XCTest“基础设施”类。
  • 受影响单元测试的其余语句实际上会执行,直到出现异常为止。
  • 包含的代码行如下所示:
    [verify(_mockTestObject)description];
  • 堆栈跟踪是关于问题发生的位置的——它说它处于设置中,但从我所知,OCMockito只是在下一个单元测试运行开始时报告预期失败。问题位置(FooTest.mm:135)与上面的verify()调用类似。
事实上,所有因此异常而失败的单元测试都会失败,因为我们正在尝试验证是否已对一个或另一个模拟对象调用了
-[NSObject description]
。删除验证该调用的所有实例将使测试通过

我在Google上搜索了这个特殊NSInternalInconsistencyException的其他实例,但没有得到任何结果。我不确定是以什么方式-[NSObject description]与任何其他可能在模拟对象上调用的方法不同-问题可能也不存在,但它只是以这种方式表现出来。我还试图找到是否有任何调试/诊断选项可以为XCTest打开,以便获得有关测试执行的详细日志信息,但我没有我也没有找到那样的东西


关于下一步应该看哪里,有什么想法吗?非常感谢!

在处理一些大型遗留代码测试时,我遇到了类似的问题。问题是:单独运行
xtexpection
测试时,它们是否工作正常?如果是,这意味着在使用
测试之前执行的一些测试ternalinconsistencexception
以相关测试完成后执行的方式分派
xcetest
相关方法

它看起来像(示例):

Test1->异步调度执行
XCTFail

测试1->完成

XCTFail
在主线程或其他线程上执行(但Test1通过,因为它完成时没有“失败”)

Test2->使用
xtexpection
->
nsinternalinconsistenceexception

苹果文档并没有提供很多关于XCTest内部细节的信息,但我很确定这就是问题所在。试试看
expectation.expectedFulfillmentCount = numberOfTimesCalled