Testing the UserSocket

Learn what phoenix channels are and write happy path and error tests for UserSocket.

What are phoenix channels?

Phoenix Channels are an abstraction of web sockets that put a certain amount of framework code on top of a somewhat open transfer protocol. They provide enough opinion on how to use web sockets that it’s easier to get going, and there’s a well-written and well-supported Phoenix JavaScript package that makes building JavaScript clients relatively easy.

Fortunately for us, Phoenix also includes plenty of test helpers (called Phoenix.ChannelTest, and pulled in by way of a generated ChannelCase module) to make testing Channel’s code fairly straightforward as well.

Note: Unlike with JSON-based APIs and server-rendered HTML, it seems useful here to talk a little more about the actual use cases of Channels to set the context of the code we’ll test. Phoenix Channels are often used in conjunction with a fully developed front-end JavaScript client.

The application included with this course uses a very small spattering of JavaScript to modify a server-rendered HTML view. In either scenario, the server-side Channel code is the concern of this book. We won’t focus on testing the JavaScript. This section won’t exhaustively show every way to test Phoenix Channel code, but it will cover enough to make it clear how testing Channels work. It’ll also dive into some of the nuances that the test tooling provided with the library specific to testing Phoenix Channels.

Get hands-on with 1200+ tech skills courses.