samedi 16 novembre 2019

get or post within ActionCable tests in Rails 6?

I am using a JWT to authenticate my user.

So when a user want to connect to the ActionCable server, it does something like this:

  • Get a JWT from REST endpoint: POST /users/sign_in
  • Establish a ws to ActionCable using this jwt.
// Establish connection
ws = new WebSocket("ws://localhost:3000/websocket?token="+jwt)

I want to test this connection so I'm using what is in Rails 6 out of the box:

# test/channels/application_cable/connection_test.rb
require "test_helper"

class ApplicationCable::ConnectionTest < ActionCable::Connection::TestCase

  test "connects with jwt" do
    post "/users/sign_in", params: { user: {email: "123@ok.com", password: "ok"}}, as: :json
    token  = response.headers["Authorization"]
    connect params: { token: token }
    assert_equal connection.user_id, "1"
  end

end

But i have the following error:

Error:
ApplicationCable::ConnectionTest#test_connects_with_jwt:
NoMethodError: undefined method `post' for #<ApplicationCable::ConnectionTest:0x0000555bbab0ecb0>
    test/channels/application_cable/connection_test.rb:6:in `block in <class:ConnectionTest>

What should I do to be able to perform this post to my controller ? Thanks!

BONUS: if I can use routes prefixes, i.e. user_session_url instead of "/users/sign_in" it's better!

Aucun commentaire:

Enregistrer un commentaire