在Rails 6的ActionCable测试中获取或发布?

我正在使用JWT对用户进行身份验证。

因此,当用户想要连接到actionCable服务器时,它会执行以下操作:

  • 从REST端点获取JWT:POST / users / sign_in
  • 使用此jwt建立到actionCable的ws。
pictureBoxList.Add((PictureBox)Controls.Find("pictureBox" + ID+1/* If your picture box is called pictureBOx1 and not pictureBox0 */,true)[0]);
pictureBoxList[ID].Location = new System.Drawing.Point(1,90);
pictureBoxList[ID].Name = "pictureBoxEnemy";
pictureBoxList[ID].Size = new System.Drawing.Size(25,25);
pictureBoxList[ID].BackgroundImage = Properties.Resources.Enemy;
pictureBoxList[ID].BackgroundImageLayout = ImageLayout.zoom;
pictureBoxList[ID].BringToFront();
ID++;

我想测试此连接,所以我可以直接使用Rails 6中的功能:

// Establish connection
ws = new WebSocket("ws://localhost:3000/websocket?token="+jwt)

但是我有以下错误:

# 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

我应该怎么做才能对控制器执行此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> ? 谢谢!

奖金:如果我可以使用路由前缀,即用post代替user_session_url,那就更好了!

qqaq99 回答:在Rails 6的ActionCable测试中获取或发布?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3090614.html

大家都在问