Created
November 17, 2021 00:15
-
-
Save faloi/f837b06d0eb7654e390a3666e3fd4de7 to your computer and use it in GitHub Desktop.
Kotlin - Ejemplo de uso de mockk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ar.edu.unahur.obj2.impostoresPaises.modelo | |
import ar.edu.unahur.obj2.impostoresPaises.api.CurrencyConverterAPI | |
import ar.edu.unahur.obj2.impostoresPaises.cli.Consola | |
class Cartelera( | |
val divisa: String, | |
var currencyApi: CurrencyConverterAPI // Inyección de dependencias (DI) | |
) { | |
var consola = Consola | |
fun imprimirMensaje() { | |
consola.escribirLinea("Para comprar un dólar se necesitan ${buscarCotizacion(divisa)} $divisa.") | |
} | |
fun buscarCotizacion(divisa: String) = currencyApi.convertirDolarA(divisa) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ar.edu.unahur.obj2.impostoresPaises.modelo | |
import ar.edu.unahur.obj2.impostoresPaises.api.CurrencyConverterAPI | |
import ar.edu.unahur.obj2.impostoresPaises.api.RestCountriesAPI | |
import ar.edu.unahur.obj2.impostoresPaises.cli.Consola | |
import io.kotest.core.spec.style.DescribeSpec | |
import io.kotest.core.spec.style.FunSpec | |
import io.kotest.matchers.shouldBe | |
import io.mockk.* | |
class CarteleraTest : DescribeSpec({ | |
describe("Cartelera") { | |
val apiMock = mockk<CurrencyConverterAPI>() | |
val consolaMock = mockk<Consola>() | |
// (1) Inyectar el mock. | |
// O por el constructor | |
val carteleraPesos = Cartelera("ARS", apiMock) | |
// O cambiando el valor de un atributo | |
carteleraPesos.consola = consolaMock | |
// (2) Configurar qué hace ANTES de que se ejecute el llamado. | |
every { apiMock.convertirDolarA("ARS") } returns 100.0 | |
every { consolaMock.escribirLinea(any()) } just Runs | |
it("devuelve la cotización del día") { | |
carteleraPesos.imprimirMensaje() | |
// (3 - opcional) Verificar que se llamó al mock. | |
verify { consolaMock.escribirLinea("Para comprar un dólar se necesitan 100.0 ARS.") } | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment