在代码中构建布局,在我需要设置地图区域之前一切正常:
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
的调用是否未立即设置,因为它是异步的?
答案 0 :(得分:0)
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)
}
}