-
Notifications
You must be signed in to change notification settings - Fork 35
On testing Cmd.batch in update functions: Expect.cmd
?
#220
Comments
It's hard to test against You could define your own and use that, or any kind of non-opaque datatype, and test against that. It wouldn't quite match the semantics of |
Yes, continuing my exploration I also realised that while testing an update command, you always want to test the batch as a whole when you come to it. So doing an Thanks, feel free to close this if there is no more segue. |
For future reference, what did you end up doing? Can you compare |
I investigated equality with module CmdEqualitySpec exposing (suite)
import Expect
import Test exposing (..)
import Task
import Http
type Msg = TestMsg String | NewBook (Result Http.Error String)
taskCmd =
Task.perform TestMsg (Task.succeed "a task the succeeds")
taskCmd2 =
Task.perform TestMsg (Task.succeed "a task the succeeds")
httpCmd =
Http.send NewBook <|
Http.getString "https://example.com/books/war-and-peace.md"
suite : Test
suite =
describe "Testing of Cmd's"
[ test "A command is equal to itself" <|
\() -> taskCmd
|> Expect.equal taskCmd
, test "A command is equal to an identical command" <|
\() -> taskCmd
|> Expect.equal taskCmd2
, test "Cmd.none is equal to its self" <|
\() -> Cmd.none
|> Expect.equal Cmd.none
, test "The Cmd.map identity function preserves equality" <|
\() -> Cmd.none
|> Expect.equal (Cmd.map identity Cmd.none)
, test "A Command is equal to the batched version of itself" <|
\() -> Cmd.none
|> List.singleton
|> Cmd.batch
|> Expect.equal Cmd.none
, test "Commands batched in the same order are equal" <|
\() -> [ taskCmd, httpCmd ]
|> Cmd.batch
|> Expect.equal (Cmd.batch [ taskCmd, httpCmd ])
, test "Commands batched in different orders are equal" <|
\() -> [ taskCmd, httpCmd ]
|> Cmd.batch
|> Expect.equal (Cmd.batch [ httpCmd, taskCmd ])
] gives the following output elm-test 0.18.12 ---------------- Running 8 tests. To reproduce these results, run: elm-test --fuzz 100 --seed 1519579438 ↓ CmdEqualitySpec ↓ test of Cmd's ✗ Single Commands are equal to identical commands This test failed because it threw an exception: "Error: Trying to use `(==)` on functions. There is no way to know if functions are "the same" in the Elm sense. Read more about this at http://package.elm-lang.org/packages/elm-lang/core/latest/Basics#== which describes why it is this way and what the better version will look like." ↓ CmdEqualitySpec ↓ test of Cmd's ✗ The Cmd.map identity function preserves equality { type = "node", branches = [] } ╷ │ Expect.equal ╵ { type = "map", tagger = <function>, tree = { type = "node", branches = [] } } ↓ CmdEqualitySpec ↓ test of Cmd's ✗ Single Commands are equal to the batched version of themselves { type = "node", branches = [{ type = "leaf", home = "Task", value = Perform <task> }] } ╷ │ Expect.equal ╵ { type = "leaf", home = "Task", value = Perform <task> } ↓ CmdEqualitySpec ↓ test of Cmd's ✗ Cmd.none is equal to the batched version of its self { type = "node", branches = [{ type = "node", branches = [] }] } ╷ │ Expect.equal ╵ { type = "node", branches = [] } ↓ CmdEqualitySpec ↓ test of Cmd's ✗ Batched commands in a different order are equal { type = "node", branches = [{ type = "leaf", home = "Task", value = Perform <task> },{ type = "leaf", home = "Task", value = Perform <task> }] } ╷ │ Expect.equal ╵ { type = "node", branches = [{ type = "leaf", home = "Task", value = Perform <task> },{ type = "leaf", home = "Task", value = Perform <task> }] } TEST RUN FAILED Duration: 188 ms Passed: 3 Failed: 5
|
So
|
Hi all!
This is more of a question but I am adding a proposal to have it be issue worthy :P
I would like to test an
update
function so that I am sure that a specific case generates a specific command. The problem is that the command is in abatch
.How would you go about it? Would it make sense to add an
Expect.cmd
, which would match both a Cmd to Cmd and a Cmd inside a batch?Thanks!
The text was updated successfully, but these errors were encountered: