Search code examples
elm

How to write a test for an http get request in elm


I'm trying to check the data received after sending a request and for that I need to write a test to make sure of it.

These are the three functions handling the data.

fetchConfigs : Cmd Msg
fetchConfigs =
Http.get
  { url = "http://localhost:8080/api/sso/configs"
    , expect = Http.expectJson GotConfigs responseDecoder
  }


responseDecoder : Decoder Config
responseDecoder =
    map2 Config
        (field "identifier" string)
        (field "ssoType" int)

type Msg
    = GotConfigs (Result Http.Error Config)

Solution

  • Have a look at https://package.elm-lang.org/packages/avh4/elm-program-test/latest/ProgramTest. From the package description:

    This module allows you to interact with your program by simulating user interactions and external events (like HTTP responses and ports), and making assertions about the HTML it renders and the external requests it makes.