How should I test my controlles while I'm using turbo?


I’m creating a project with devise and I have the register controller. who responds with status :unprocessable_entity so Turbo can change the html. (In other cases, devise would return a 200 status and render the same registration page)

Right know my test looks something like this:

  test "test something" do
    post user_registration_path, headers: {Accept: 'text/vnd.turbo-stream.html'}

    assert_response :unprocessable_entity

And now I have to know which actions are expected to be load by turbo and add that header to those test. Is this the expected way for testing Rails with Turbo?

I feel like I’m missing something. At the end, if for some reason JS dies, my page would still keep working because the form can be sent and the server would responde with the full html. Should I keep those behaviours in mind as well?

Does :point_down: work?

post user_registration_path, xhr: true
1 Like

No, it doesn’t.

In that case the controller is going to return a 200 status (instead of unprocessable_entity) and Turbo is going to fail (because turbo expect a return instead of ok when Post request are handled without errors)

Have you tried post user_registration_path(format: :turbo_stream)?


Thanks a ton for that!

For a request spec in Rails 6 I needed to add subaction: :refresh for a put request. So:

put user_registration_path(format: :turbo_stream, subaction: :refresh)