我正在尝试验证我存储的两个项目是否相同。但是,在测试时,我在检查Date属性时遇到错误。
注意:我的Item类实现了Equatable
协议。
这是我的setUp方法:
class InputViewControllerTests: XCTestCase {
var sut: InputViewController!
var placemark: MockPlacemark!
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Main",
bundle: nil)
sut = storyboard
.instantiateViewController(
withIdentifier: "InputViewController")
as! InputViewController
_ = sut.view
}
}
这是我的测试类的扩展:
extension InputViewControllerTests {
class MockGeocoder: CLGeocoder {
var completionHandler: CLGeocodeCompletionHandler?
override func geocodeAddressString(
_ addressString: String,
completionHandler: @escaping CLGeocodeCompletionHandler) {
self.completionHandler = completionHandler
}
}
class MockPlacemark : CLPlacemark {
var mockCoordinate: CLLocationCoordinate2D?
override var location: CLLocation? {
guard let coordinate = mockCoordinate else
{ return CLLocation() }
return CLLocation(latitude: coordinate.latitude,
longitude: coordinate.longitude)
}
}
}
这是我的测试:
func test_Save_UsesGeocoderToGetCoordinateFromAddress() {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
let timestamp = 1456095600.0
let date = Date(timeIntervalSince1970: timestamp)
sut.titleTextField.text = "Foo"
sut.dateTextField.text = dateFormatter.string(from: date)
sut.locationTextField.text = "Bar"
sut.addressTextField.text = "Infinite Loop 1, Cupertino"
sut.descriptionTextField.text = "Baz"
let mockGeocoder = MockGeocoder()
sut.geocoder = mockGeocoder
sut.itemManager = ItemManager()
sut.save()
placemark = MockPlacemark()
let coordinate = CLLocationCoordinate2DMake(37.3316851,
-122.0300674)
placemark.mockCoordinate = coordinate
mockGeocoder.completionHandler?([placemark], nil)
let item = sut.itemManager?.item(at: 0)
let testItem = ToDoItem(title: "Foo",
itemDescription: "Baz",
timestamp: timestamp,
location: Location(name: "Bar",
coordinate: coordinate))
XCTAssertEqual(item, testItem)
}
这是save()
方法的实现:
class InputViewController: UIViewController {
// ...
@IBAction func save() {
guard let titleString = titleTextField.text,
titleString.characters.count > 0 else { return }
let date: Date?
if let dateText = self.dateTextField.text,
dateText.characters.count > 0 {
date = dateFormatter.date(from: dateText)
} else {
date = nil
}
let descriptionString = descriptionTextField.text
if let locationName = locationTextField.text,
locationName.characters.count > 0 {
if let address = addressTextField.text,
address.characters.count > 0 {
geocoder.geocodeAddressString(address) {
[unowned self] (placeMarks, error) -> Void in
let placeMark = placeMarks?.first
let item = ToDoItem(
title: titleString,
itemDescription: descriptionString,
timestamp: date?.timeIntervalSince1970,
location: Location(
name: locationName,
coordinate: placeMark?.location?.coordinate))
self.itemManager?.add(item)
}
}
}
}
}
我无法弄清楚这有什么问题。我得到的错误是:
test_Save_UsesGeocoderToGetCoordinateFromAddress()]失败:XCTAssertEqual失败:("可选(ToDo.ToDoItem(标题:" Foo",itemDescription:可选(" Baz"),时间戳:可选(1456030800.0),位置:可选(ToDo.Location(名称:"栏",坐标:可选(__ C.CLLocationCoordinate2D(纬度:37.331685100000001,经度:-122.03006739999999)))))" )不等于("可选(ToDo.ToDoItem(标题:" Foo",itemDescription:可选(" Baz"),时间戳:可选(1456095600.0),位置:可选(ToDo.Location(名称:"栏",坐标:可选(__ C.CLLocationCoordinate2D(纬度:37.331685100000001,经度:-122.03006739999999))))))") -
可以清楚地看到,问题是两者的时间戳不一样,我不知道它为什么会改变。
编辑:正如@ganzogo发现的那样,这个项目之间的差异恰好是18个小时。我住在厄瓜多尔,这是GTM-5。也许这可以解决问题。
答案 0 :(得分:1)
你的行后:
let dateFormatter = DateFormatter()
试试这个:
dateFormatter.timeZone = TimeZone(secondsFromGMT: 64800)
如果不起作用,请尝试:
dateFormatter.timeZone = TimeZone(secondsFromGMT: -64800)
: - )
但如果你只是在通行证中进行黑客攻击,那么你就有点打败单位测试的目的。您真的需要了解testItem
或item
现在是否表现出正确的行为,这取决于您的申请。
答案 1 :(得分:1)
在func中
test_Save_UsesGeocoderToGetCoordinateFromAddress()
然后将时间戳转换为sut.dateTextField.text
在sut.save()
sut.dateTextField.text
转换为时间戳,分钟和秒钟都会下降