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)

HELLO, If you’re using Turbo, a platform that allows you to build and test APIs and web applications, you can test your controllers in several ways, including the following:

  1. Test Driven Development (TDD): Write test cases for your controllers before writing the actual code. This will help you verify that your controllers behave as expected.
  2. Unit Tests: Write unit tests for individual functions within your controllers. Unit tests should test specific functionality and should not depend on other parts of the system.
  3. Integration Tests: Write integration tests to verify that your controllers work correctly with other parts of the system, such as databases or APIs.
  4. End-to-end Tests: Write end-to-end tests to verify that the entire system, including the controllers, works as expected from the user’s perspective.
  5. Use a Testing Framework: Use a testing framework such as Go’s built-in testing package or a third-party library such as Ginkgo to simplify the process of writing and running tests.

These testing methods will help you ensure that your controllers behave as expected and that your application is working correctly. Note that the specifics of these testing methods may vary depending on the programming language and framework you are using. This is the best way to test controlled using turbo and it will perfectly work.