Skip to content

Add a few client tests#82

Merged
bitwalker merged 1 commit intobitwalker:masterfrom
jussih:client-test
Oct 27, 2018
Merged

Add a few client tests#82
bitwalker merged 1 commit intobitwalker:masterfrom
jussih:client-test

Conversation

@jussih
Copy link
Contributor

@jussih jussih commented Oct 25, 2018

I noticed there are hardly any tests for the client code. I was thinking about how to test genservers in Elixir and this is what I came up with. I call handle_data and expect a message to the handler process, which is actually the ExUnit process itself.

I'm open for discussion. Does this make any sense? Are there better conventions for testing in Elixir?

@coveralls
Copy link

Coverage Status

Coverage increased (+3.4%) to 34.267% when pulling 7b08539 on jussih:client-test into 295a72d on bitwalker:master.

2 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+3.4%) to 34.267% when pulling 7b08539 on jussih:client-test into 295a72d on bitwalker:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+3.4%) to 34.267% when pulling 7b08539 on jussih:client-test into 295a72d on bitwalker:master.

@bitwalker
Copy link
Owner

This works for the Client module, but doesn't work in general for testing GenServers, the usual approach is to start the process, and send messages/expect replies or examine the server state:

me = self()
{:ok, pid} = Agent.start_link(fn -> 1 end)
Agent.update(pid, fn n -> n + 1 end)
Agent.update(pid, fn n -> send(me, {:n, n}); n end)
assert 2 = :sys.get_state(pid)
assert_receive {:n, 2}

The above example is trivial, but hopefully illustrates the point. Agent is built on GenServer, and we're doing two things: using :sys.get_state/1 to get the internal state of the server so we can examine it for the test, and asserting on receiving a message. The latter can be useful when you expect a server to send a response to a caller, which in tests is going to be the test process.

For this PR, it is not necessary to do that, but in general that's the usual approach :)

@bitwalker bitwalker merged commit dfa341b into bitwalker:master Oct 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants