gpt4 book ai didi

ios - 断言日期时测试失败

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:04 25 4
gpt4 key购买 nike

我正在尝试验证我存储的两个项目是否相同。但是,在测试期间,我在检查 Date 属性时遇到错误。

注意:我的 Item 类实现了 Equatable 协议(protocol)。

这是我的设置方法:

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()] failed: XCTAssertEqual failed: ("Optional(ToDo.ToDoItem(title: "Foo", itemDescription: Optional("Baz"), timestamp: Optional(1456030800.0), location: Optional(ToDo.Location(name: "Bar", coordinate: Optional(__C.CLLocationCoordinate2D(latitude: 37.331685100000001, longitude: -122.03006739999999))))))") is not equal to ("Optional(ToDo.ToDoItem(title: "Foo", itemDescription: Optional("Baz"), timestamp: Optional(1456095600.0), location: Optional(ToDo.Location(name: "Bar", coordinate: Optional(__C.CLLocationCoordinate2D(latitude: 37.331685100000001, longitude: -122.03006739999999))))))") -

可以清楚地看到,问题是两者的时间戳不一样,我不知道为什么会发生变化。

编辑:正如@ganzogo 所发现的,这两个项目之间也存在 18 小时的差异。我住在 GTM-5 的厄瓜多尔。也许这可能是找出问题的线索。

最佳答案

在你的行之后:

let dateFormatter = DateFormatter()

试试这个:

dateFormatter.timeZone = TimeZone(secondsFromGMT: 64800)

如果还是不行,试试:

dateFormatter.timeZone = TimeZone(secondsFromGMT: -64800)

:-)

但是,如果您只是对单元测试进行修改直到通过,那么您就有点违背了单元测试的目的。您确实需要了解 testItemitem 现在是否表现出正确的行为,这取决于您的应用程序。

关于ios - 断言日期时测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com