代码之家  ›  专栏  ›  技术社区  ›  Johan

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

  •  1
  • Johan  · 技术社区  · 6 年前

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

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

    我的榆树模型是这样的:

    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!
    

    前端.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 回复  |  直到 6 年前
        1
  •  2
  •   J.J. Hakala    6 年前

    module FrontendTest exposing (..)
    

    应该解决这个问题。