Layout dependence in Elm

elmImported dependencies are declared in each file . Is there a way, while checking the application, to mock the addiction?

For example, suppose I have an application that uses an HTTP module to create an ajax request. When I test my module, I would like to avoid the actual ajax request, but I would like to have a mocked HTTP module that will return a fake response just for testing.

How can i do this?

+4
source share
2 answers

Elm - , - , . , .

HTTP-, Action:

type alias MyThing =
  { id : Int
  , name : String
  }

type Action
  = FetchData
  | ErrorOccurred String
  | DataFetched MyThing

myDecoder : Json.Decoder MyThing
myDecoder =
  Json.object2
    MyThing
    ("id" := Json.int) 
    ("name" := Json.string) 

fetchData : Effects Action
fetchData =
  Http.get myDecoder url
    |> Task.toResult
    |> Task.map httpResultToAction
    |> Effects.task

httpResultToAction : Result Http.Error MyThing -> Action
httpResultToAction result =
  case result of
    Ok thing ->
      DataFetched thing
    Err err ->
      ErrorOccurred (toString err)

, , .

JSON myDecoder. JSON Json.Decode.decodeString myDecoder , .

, , , , httpResultToAction. , . , , Result .

+7

, Http-, , .

, , Result Error String (, Http.getString), unit test HTTP-.

+1

Source: https://habr.com/ru/post/1626882/


All Articles