如何在Elm中为http get请求编写测试

问题描述

我正在尝试检查发送请求后收到的数据,为此,我需要编写一个测试来确保这一点。

这是处理数据的三个功能

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)

解决方法

看看https://package.elm-lang.org/packages/avh4/elm-program-test/latest/ProgramTest。从包装说明中:

此模块允许您通过模拟与程序进行交互 用户互动和外部事件(例如HTTP响应和端口), 并对其所呈现的HTML和外部 请求它做。