swift - 设置 MKMapKit 区域在测试中失败

标签 swift mapkit xctest

在代码中构建布局,一切都很好,直到我需要设置 map 区域:

    if let coordinate = item.location?.coordinate {

        let region = MKCoordinateRegionMakeWithDistance(coordinate, 100, 100)
        let span = MKCoordinateSpanMake(coordinate.latitude, coordinate.longitude)
        let regionM = MKCoordinateRegionMake(coordinate, span)
        print(mapView.centerCoordinate)
        print(coordinate)
        print(region)

        mapView.region = region
        mapView.region.center = coordinate
        mapView.regionThatFits(region)
        mapView.setCenter(coordinate, animated: false)
        mapView.setRegion(region, animated: false)
        mapView.setRegion(regionM, animated: false)
        print(mapView.region)
        print(region)

    }

这是失败的测试:

func test_SettingItemInfo_SetsMapLocation() {
    let coordinate = CLLocationCoordinate2DMake(51.2277, 6.7735)
    let location = Location(name: "Cool", coordinate: coordinate)

    let item = Item(title: "Here", location: location)
    manager.add(item)
    sut.info = (manager, 0)

    sut.beginAppearanceTransition(true, animated: true)
    sut.endAppearanceTransition()

    // possibly asynchronous
    XCTAssertEqual(sut.mapView.centerCoordinate.latitude, coordinate.latitude, accuracy: 0.001)
    XCTAssertEqual(sut.mapView.centerCoordinate.longitude, coordinate.longitude, accuracy: 0.001)
}

这是打印日志的结果:

CLLocationCoordinate2D(latitude: 37.787685747888986, longitude: -122.40971999999998)
CLLocationCoordinate2D(latitude: 51.227699999999999, longitude: 6.7735000000000003)
MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: 51.227699999999999, longitude: 6.7735000000000003), span: __C.MKCoordinateSpan(latitudeDelta: 0.00089885544064104096, longitudeDelta: 0.0014315673851581932))
MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: 37.787685747888986, longitude: -122.40971999999998), span: __C.MKCoordinateSpan(latitudeDelta: 90.0, longitudeDelta: 180.0))
MKCoordinateRegion(center: __C.CLLocationCoordinate2D(latitude: 51.227699999999999, longitude: 6.7735000000000003), span: __C.MKCoordinateSpan(latitudeDelta: 0.00089885544064104096, longitudeDelta: 0.0014315673851581932))

这是失败的测试日志:

error: -[TDDS4ToDoTests.DetailViewControllerTests test_SettingItemInfo_SetsMapLocation] : XCTAssertEqualWithAccuracy failed: ("37.787685747889") is not equal to ("51.2277") +/- ("0.001") - 
error: -[TDDS4ToDoTests.DetailViewControllerTests test_SettingItemInfo_SetsMapLocation] : XCTAssertEqualWithAccuracy failed: ("-122.40972") is not equal to ("6.7735") +/- ("0.001") - 
Test Case '-[TDDS4ToDoTests.DetailViewControllerTests test_SettingItemInfo_SetsMapLocation]' failed (213.944 seconds).

理想情况下,第一行应该修复该区域,但我尝试了几种不同的方法(如上所示)。 对 mapview.region 的调用是否不会立即设置,因为它是异步的?

最佳答案

MKMapView 中的几乎所有内容都是异步更新的。您需要使用 XCTestExpectation (或类似的)来监听 MKMapViewDelegate 回调。

这是一个令人难以置信的乏味和困难的测试 - 充满了微妙的陷阱。将这个独立的测试放在这里供后代使用:

import MapKit
import XCTest

/// a callback to pipe region changes into our test
protocol MapViewControllerDelegate: class {
    func regionChanged(to mapRect: MKMapRect)
}

/// the view controller we want to test
class MapViewController: UIViewController, MKMapViewDelegate {

    weak var delegate: MapViewControllerDelegate?

    var mapView: MKMapView {
        return view as! MKMapView
    }

    override func loadView() {
        let mapView = MKMapView()
        mapView.delegate = self
        view = mapView
    }

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        delegate?.regionChanged(to: mapView.visibleMapRect)
    }

}

/// a "Spy pattern"-like object for capturing the inputs we want to validate with our test
class Delegate: MapViewControllerDelegate {

    var mapRect: MKMapRect?
    var regionChangedExpectation: XCTestExpectation?

    func regionChanged(to mapRect: MKMapRect) {
        guard let regionChangedExpectation = regionChangedExpectation else {
            XCTFail("Delegate was not setup correctly. Missing expectation reference")
            return
        }

        self.mapRect = mapRect
        regionChangedExpectation.fulfill()
    }
}

/// our tests
class TestCase: XCTestCase {

    var viewController: MapViewController!

    override func setUp() {
        super.setUp()

        viewController = MapViewController()

        // force the view to load
        _ = viewController.view

        // VERY IMPORTANT: If you don't put the map view in a window none of the delegate callbacks get fired.
        // I assume this is some kind of MapKit optimization.
        UIApplication.shared.keyWindow?.rootViewController = viewController
    }

    func testExample() {
        // set up an expectation to test the async call
        let regionChangedExpectation = expectation(description: "Should update region")
        regionChangedExpectation.expectedFulfillmentCount = 1

        // create our Spy object to stand in for our usual delegate
        let delegate = Delegate()
        delegate.regionChangedExpectation = regionChangedExpectation
        viewController.delegate = delegate

        // the test region
        var firstRect = MKMapRect(origin: MKMapPoint(x: 61089821.0, y: 110138019.04306149),
                                              size: MKMapSize(width: 361250.00249999744, height: 691590.91387700045))

        // adjust the mapRect so it fits the mapView
        firstRect = viewController.mapView.mapRectThatFits(firstRect, edgePadding: .zero)

        // trigger a map viewport change
        viewController.mapView.visibleMapRect = firstRect

        // block and wait for the async call to finish
        wait(for: [regionChangedExpectation], timeout: 10)

        // make sure we received the region change
        guard let mapRect = delegate.mapRect else {
            XCTFail("no visible map rect set")
            return
        }

        // make sure we received the RIGHT region change
        XCTAssertEqual(firstRect.origin.y, mapRect.origin.y, accuracy: 0.002)
        XCTAssertEqual(firstRect.origin.x, mapRect.origin.x, accuracy: 0.002)
        XCTAssertEqual(firstRect.size.width, mapRect.size.width, accuracy: 0.002)
        XCTAssertEqual(firstRect.size.height, mapRect.size.height, accuracy: 0.002)
    }

}

关于swift - 设置 MKMapKit 区域在测试中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993072/

相关文章:

objective-c - 在 map View 上绘制 MKCircle

ios - 在 iOS 中绘制多点路线

uiwebview - 使用 Xcode UI 测试测试 UIWebView

ios - Swift - 从容器中的 TableView Controller 展开

ios - 如何让 UIWebView 像 WKWebView 一样自动适应内容?

objective-c - OSX 应用程序在某些机器上打开,而不是其他机器

ios - UISearchController 不触发委托(delegate)方法和搜索栏委托(delegate)方法

iphone - ios Mapkit : fix annotation on middle of map

Cocoapods - 仅包含 XCTest 文件的某些依赖项

code-coverage - guard 声明后的 XCTest 覆盖率