Benjamin-Philip
Get mix task output so far for tests
I am writing a new (sub)task for the hex client, which I have some trouble testing.
The task has some output, using which I need to make a HTTP POST request (in the test).
The task will only complete after I make the HTTP request.
Now, how do I get the mix output so far, so that I can write tests for this task.
I have noticed that in the hex tests that prompt inputs are mocked as follows:
https://github.com/hexpm/hex/blob/main/test/mix/tasks/hex.user_test.exs#L53-L57
Perhaps I can send a :mix_shell_output msg and receive a response?
PS: I also didn’t find any docs for this idiom.
Marked As Solved
stefanchrobot
Sorry for the confusion. I did misread the question. Seems Mix.Shell.Process is exactly what you need:
This is mainly useful in tests, allowing us to assert if given messages were received or not instead of performing checks on some captured IO. Since we need to guarantee a clean slate between tests, there is also a
flush/1function responsible for flushing all:mix_shellrelated messages from the process inbox.
It’s not Hex-specific at all.
Also Liked
Benjamin-Philip
I talked to Eric (@ericmj) about this.
Turns out that all mix shell output is sent to the test process.
That means I can for example do things like this:
recieve do
{:mix_shell, :info, [msg | _ ]} -> IO.inspect(msg)
end
Or an assert_recieved:
assert_received {:mix_shell, :info, ["Some description\n"]}
Do note that I am unsure if this solution is hex specific or not.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









