使用 Rspec 测试 Turbo 流操作

Syl*_*Syl 3 rspec ruby-on-rails turbo

我正在对使用涡轮流的控制器操作进行 rspec 测试:

  describe 'GET /CONTROLLER_NAME' do

    it 'return a turbo stream answer' do
      get :index, as: :turbo_stream
      expect(response).to eq Mime[:turbo_stream]
    end
  end

end

Run Code Online (Sandbox Code Playgroud)
Failure/Error: expect(response).to eq Mime[:turbo_stream]

       expected: #<Mime::Type:0x00007f7d9c2e1ff0 @synonyms=[], @symbol=:turbo_stream, @string="text/vnd.turbo-stream.html", @hash=2866392594387537360>
            got: #<ActionDispatch::TestResponse:0x00007f7d976945f8 @mon_data=#<Monitor:0x00007f7d97694580>, @mon_data_...oller::TestRequest GET "http://test.host/CONTROLLER_NAME.turbo_stream" for 0.0.0.0>>
Run Code Online (Sandbox Code Playgroud)

如何在控制器测试中使用 Turbo Stream 进行获取查询?

Sam*_*dhe 12

你应该检查response.media_typeMime[:turbo_stream]而不是仅仅request。检查涡轮流测试助手

  describe 'GET /CONTROLLER_NAME' do
    it 'return a turbo stream answer' do
      get :index, as: :turbo_stream
      expect(response.media_type).to eq Mime[:turbo_stream]
    end
  end

Run Code Online (Sandbox Code Playgroud)