Testing Phoenix channels: How to test changes to the socket assigns done in handle_in/3 and get the updated socket?

I might be approaching this in the wrong way. I have a handle_in/3 call that would change the socket assigns. I see that Phoenix.ChannelTest provides a push function. However, the problem is that this function seems to only return a ref, without returning the updated socket struct on the channel side in any way. How can I check whether the socket assigns have been updated successfully, and also, how can I use the updated socket in later parts of the same test?

The channel test api is not a functional API. You can just reuse the socket variable you got to push multiple messages.

Interesting. Seemed to me the socket assigns were not changed, but maybe my code was not behaving as expected. I’ll take a look again.

They’re not supposed to change. You’d want to test sideeffects like db inserts or sending another message to query the data again.

1 Like

Understood. Guess if I want to emulate the effect of a message changing the assigns before testing another message, I’d probably need to manually update the assigns then.

I don’t think you want to assert anything based on the data in that socket variable. It’s mostly there to provide various pids to other channel test API.

1 Like

OK I guess what you’re emphasizing here is that since this is not a real socket during the test, but rather a construct to facilitate testing, I shouldn’t expect to treat it like how a real socket behaves, but should think about how to properly construct a test to test what specifically matters in that unit test. Does this make sense?

Yeah, for the most part.

Technically the socket you get is the initial socket used to start the channel process with, but it won’t magically inherit all the changes to you do it over the lifetime of the channel instance being alive.