Elm测试说模块声明无效,但是为什么呢?

时间:2018-09-30 10:59:28

标签: elm elm-test

我对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上找到代码。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

Elm模块应该在文件顶部(即

)具有模块声明
C:\fakepath\imageName.png

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