在Kotlin单元测试中创建MKMapView时崩溃

时间:2020-07-14 13:38:11

标签: kotlin mapkit kotlin-multiplatform

我正在尝试将测试添加到使用Mapbox的kotlin多平台库的一部分中,但无法实例化MKMapView的实例。

即使是像崩溃一样简单的测试。

<Form>

输出为

@Test
fun `test creating view`() {
    MKMapView(CGRectMake(0.0, 0.0, 512.0, 512.0))
}

在模拟器上运行真实应用程序时,我能够正确创建MKMapView。我猜这与kotlin测试用于运行测试的环境有关,但是我不知道如何配置它。

我正在将Kotlin 1.3.72与gradle插件的内置iOS测试任务结合使用。

1 个答案:

答案 0 :(得分:1)

这里的根本原因是Kotlin / Native使用xcrun simctl spawn CLI工具来运行测试。 此命令在模拟器上运行给定的测试,而不是作为应用程序而是作为可执行文件运行。这使测试更加简单,但是某些极端情况可能会出错。可能是,我们在负责图形的设备服务方面遇到了问题。在这种假设下,我依靠stacktrace中的1)Error creating notification handler for simulator graphics quality override: 3错误消息和2)initShouldRasterize,对我来说似乎很可疑。


为了证明该问题与Kotlin / Native没有直接关系,我编写了两个代码段:map.swift和map.kt。 像这样编译它们: swiftc map.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.5.sdk -target x86_64-apple-ios13.5 ~/.konan/kotlin-native-macos-1.3.72/bin/konanc -tr -g -ea map.kt -target ios_x64 -o map 创建两个可执行文件:mapmap.kexe。之后,我使用simctl spawn来运行它们:

xcrun simctl spawn -s "C01BDD64-19DC-49A0-9363-92EAFFD1B258" <absolute_path>/map
2020-07-15 12:49:57.262 map[8840:1053205] Error creating notification handler for simulator graphics quality override: 3
2020-07-15 12:49:57.264 map[8840:1053205] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'contentScale is unreasonable (NaN or Inf)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23e3cf0e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23e3cad9 -[NSException raise] + 9
    3   VectorKit                           0x00007fff49c14143 _Z25checkForBogusContentScaled + 115
    4   VectorKit                           0x00007fff49c14252 -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:] + 62
    5   MapKit                              0x00007fff2782009a -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 419
    6   MapKit                              0x00007fff27755b85 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:showsAppleLogo:] + 1331
    7   MapKit                              0x00007fff27756698 -[MKMapView initWithFrame:] + 253
    8   map                                 0x000000010b924f0d $sSo9MKMapViewC5frameABSo6CGRectV_tcfcTO + 77
    9   map                                 0x000000010b924eaa $sSo9MKMapViewC5frameABSo6CGRectV_tcfC + 74
    10  map                                 0x000000010b924e07 main + 183
    11  libdyld.dylib                       0x00007fff51a231fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Child process terminated with signal 6: Abort trap
xcrun simctl spawn -s "C01BDD64-19DC-49A0-9363-92EAFFD1B258" <absolute_path>/map.kexe
[==========] Running 1 tests from 1 test cases.
[----------] Global test environment set-up.
[----------] 1 tests from MapKt
[ RUN      ] MapKt.test creating view
2020-07-15 13:49:19.079 map.kexe[9117:1072135] Error creating notification handler for simulator graphics quality override: 3
2020-07-15 13:49:19.082 map.kexe[9117:1072135] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'contentScale is unreasonable (NaN or Inf)'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23e3cf0e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50ba89b2 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23e3cad9 -[NSException raise] + 9
    3   VectorKit                           0x00007fff49c14143 _Z25checkForBogusContentScaled + 115
    4   VectorKit                           0x00007fff49c14252 -[VKMapView initShouldRasterize:inBackground:contentScale:auditToken:] + 62
    5   MapKit                              0x00007fff2782009a -[MKBasicMapView initWithFrame:andGlobe:shouldRasterize:] + 419
    6   MapKit                              0x00007fff27755b85 -[MKMapView _commonInitFromIB:gestureRecognizerHostView:showsAttribution:showsAppleLogo:] + 1331
    7   MapKit                              0x00007fff27756698 -[MKMapView initWithFrame:] + 253
    8   map.kexe                            0x000000010e29c530 _6d6170_knbridge0 + 64
    9   map.kexe                            0x000000010e293605 kfun:test creating view() + 1077
    10  map.kexe                            0x000000010e2938f5 kfun:$test creating view$FUNCTION_REFERENCE$0.invoke#internal + 37
    11  map.kexe                            0x000000010e29394e kfun:$test creating view$FUNCTION_REFERENCE$0.$<bridge-UNN>invoke()#internal + 62
    12  map.kexe                            0x000000010e206218 kfun:kotlin.native.internal.test.TopLevelSuite.TestCase.run() + 1112
    13  map.kexe                            0x000000010e1fe73d kfun:kotlin.native.internal.test.TestRunner.run#internal + 2445
    14  map.kexe                            0x000000010e1ff8ea kfun:kotlin.native.internal.test.TestRunner.runIteration#internal + 2954
    15  map.kexe                            0x000000010e200215 kfun:kotlin.native.internal.test.TestRunner.run()kotlin.Int + 997
    16  map.kexe                            0x000000010e1f3826 kfun:kotlin.native.internal.test.testLauncherEntryPoint(kotlin.Array<kotlin.String>)kotlin.Int + 310
    17  map.kexe                            0x000000010e1f36ca kfun:kotlin.native.internal.test.main(kotlin.Array<kotlin.String>) + 58
    18  map.kexe                            0x000000010e1f3975 Konan_start + 165
    19  map.kexe                            0x000000010e2a13ab Init_and_run_start + 107
    20  libdyld.dylib                       0x00007fff51a231fd start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Child process terminated with signal 6: Abort trap

尽管如此,我同意应该解决此不便之处,以使测试更加人性化。我可以在Kotlin Bug Tracker上提交问题,您可以吗?