DEV Community

David Goyes
David Goyes

Posted on

Swift Testing #10: Desencapsular un valor opcional como precondición con #require

El macro require(_:_:sourceLocation:) es una versión sobrecargada que recibe un valor opcional. Este intentará desempaquetarlo y, si es nil, fallará y arrojará un error.

@Test
func processOptionalValue() throws {
  let sut = FailingFeature()
  sut.value = 1
  let value = try #require(sut.getOptionalValue()) // ✅ La prueba pasa
  // ...
  sut.value = nil
  let value = try #require(sut.getOptionalValue()) // ❌ La prueba falla
}
Enter fullscreen mode Exit fullscreen mode

Bibliografía

Top comments (0)