1

我对 Elm 很陌生,我正在使用 Elm 0.19 开始我的第一个宠物项目。

我想在我的应用程序中创建一个 JSON 解码的测试用例。从服务器返回的 JSON 如下所示(所有内容都在名为 的文件中定义Frontend.elm):

[
    {
        "gameId": "game1",
        "player1": "player1",
        "player2": "player2",
        "winner": "player1",
        "state": "ended"
    },
    {
        "gameId": "game3",
        "player1": "player1",
        "state": "joinable"
    }
]

我的 Elm 模型如下所示:

type Player =
  Player String

type State
  = Joinable
  | Started
  | Ended

type alias Game =
  { gameId : String
  , player1 : Maybe Player
  , player2 : Maybe Player
  , winner : Maybe Player
  , state : State}

我的解码逻辑是这样定义的:

gameStateDecoder : Decode.Decoder State
gameStateDecoder =
  string
        |> andThen (\stateAsString ->
           case stateAsString of
                "joinable" ->
                    succeed Joinable
                "ended" ->
                    succeed Ended
                "started" ->
                    succeed Started
                unknown ->
                    fail <| "Unknown game state: " ++ unknown
        )


playerDecoder : Decode.Decoder (Maybe Player)
playerDecoder =
    (maybe string)
        |> andThen (\maybePlayerString ->
            succeed
              <| case maybePlayerString of
                Just player ->
                   Just (Player player)
                _ ->
                   Nothing
        )

gameListDecoder : Decode.Decoder (List Game)
gameListDecoder =
  Decode.list gameDecoder

gameDecoder : Decode.Decoder Game
gameDecoder =
   Decode.map5 Game
        (field "gameId" string)
        (field "player1" playerDecoder)
        (field "player2" playerDecoder)
        (field "winner" playerDecoder)
        (field "state" gameStateDecoder)

现在我尝试使用elm-explorations/test包为此创建一个测试用例:

import Frontend exposing (..)
import Expect exposing (equal)
import Test exposing (Test, test)
import Json.Decode exposing (decodeString)

decodesGameList : Test
decodesGameList =
    test "Properly decodes a game list" <|
        \() ->
            let
                json =
                    """
                      [
                          {
                              "gameId": "game",
                              "state": "joinable"
                          }
                      ]
                    """

                decodedOutput  =
                    decodeString gameListDecoder json
            in
                equal
                    decodedOutput
                    (Ok
                        [ Game "game" Nothing Nothing Nothing Joinable ]
                    )

但是当我尝试使用它运行它时,elm-test我得到了这个错误:

<project path>/tests/FrontendTest.elm has an invalid module declaration. Check the first line of the file and make sure it has a valid module declaration there!

我不明白我做错了什么。该模块的定义如下Frontend.elm

module Frontend exposing (gameListDecoder, State(..), Game, Player(..))

我可以使用以下方法很好地编译它:

$ elm make src/main/elm/Frontend.elm --output src/main/resources/static/index.html
Success! Compiled 1 module.

您实际上可以在github找到代码。

我该如何解决这个问题?

4

1 回答 1

2

一个 Elm 模块应该在文件的顶部有一个模块声明,即

module FrontendTest exposing (..)

在这种情况下应该解决问题。

于 2018-10-01T06:21:49.540 回答