Add RSpec matchers for turbo stream and turbo frames#2888
Open
bastin27 wants to merge 2 commits intorspec:mainfrom
Open
Add RSpec matchers for turbo stream and turbo frames#2888bastin27 wants to merge 2 commits intorspec:mainfrom
bastin27 wants to merge 2 commits intorspec:mainfrom
Conversation
… frames Rails 7+ uses Turbo as the default for form submissions and page updates, but rspec-rails had no support for testing Turbo responses. This adds: - `have_turbo_stream` matcher to assert turbo-stream elements in responses - `be_turbo_stream` matcher to check Turbo Stream content type - `have_turbo_frame` matcher to assert turbo-frame elements in responses - `has_turbo?` feature check for conditional loading - Updated scaffold generator to include turbo_stream format tests - Cucumber feature specs and RSpec unit tests
JonRowe
requested changes
Mar 9, 2026
Member
JonRowe
left a comment
There was a problem hiding this comment.
👋 Sorry for the delay in looking at this, its something thats worth having but I feel prehaps that instead we should be leveraging the Rails assertations using:
match_unless_raises ActiveSupport::TestCase::Assertion do
@scope.assert_...
end
Style checks rather than re-building them in matchers...
|
|
||
| def have_turbo_stream(**opts) | ||
| RSpec::Rails::Matchers::Turbo::HaveTurboStream.new(**opts) | ||
| end |
Member
There was a problem hiding this comment.
As above, you should be able to call this already
| RSpec.describe "have_turbo_frame matcher" do | ||
| def have_turbo_frame(id) | ||
| RSpec::Rails::Matchers::Turbo::HaveTurboFrame.new(id) | ||
| end |
Member
There was a problem hiding this comment.
This should be available already
Comment on lines
+92
to
+97
| context "with turbo_stream format", if: defined?(Turbo) do | ||
| it "creates a new <%= class_name %> and responds with turbo stream" do | ||
| post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }, as: :turbo_stream | ||
| expect(response).to be_turbo_stream | ||
| end | ||
| end |
Member
There was a problem hiding this comment.
These should be wrapped in an if statement that only emits the fragment if Turbo is enabled e.g. (note the -%> which remove the if line whitespace from the output)
Suggested change
| context "with turbo_stream format", if: defined?(Turbo) do | |
| it "creates a new <%= class_name %> and responds with turbo stream" do | |
| post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }, as: :turbo_stream | |
| expect(response).to be_turbo_stream | |
| end | |
| end | |
| <% if RSpec::Rails::FeatureCheck.has_turbo? -%> | |
| context "with turbo_stream format" do | |
| it "creates a new <%= class_name %> and responds with turbo stream" do | |
| post <%= index_helper %>_url, params: { <%= singular_table_name %>: valid_attributes }, as: :turbo_stream | |
| expect(response).to be_turbo_stream | |
| end | |
| end | |
| <% end -%> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
have_turbo_streammatcher to assert<turbo-stream>elements by action and target/targets, with optional.with_count(n)chainingbe_turbo_streammatcher to verify Turbo Stream content type (text/vnd.turbo-stream.html)have_turbo_framematcher to assert<turbo-frame>elements by idhas_turbo?feature check for conditional loading whenturbo-railsis availableturbo_streamformat tests for create, update, and destroy actionsTest plan
turbo-railsavailableRelated issue: #2887