From d6792b434fbb17d49679660112e402f2b56040b7 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Sat, 10 Aug 2024 18:23:42 -0400 Subject: [PATCH] Extract `cell_selector` method in scaffold index view spec generator (#2777) Move conditional `cell_selector` out of generated view spec --- Changelog.md | 1 + lib/generators/rspec/scaffold/templates/index_spec.rb | 2 +- spec/generators/rspec/scaffold/scaffold_generator_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index f93d6f0ce..7dc501018 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ Bug Fixes: * Prevent `have_http_status` matcher raising an error when encountering a raw `Rack::MockResponse`. (Christophe Bliard, #2771) +* Move Rails version conditional from index scaffold generated file to template. (Matt Jankowski, #2777) ### 6.1.3 / 2024-06-19 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v6.1.2...v6.1.3) diff --git a/lib/generators/rspec/scaffold/templates/index_spec.rb b/lib/generators/rspec/scaffold/templates/index_spec.rb index 4abbc0da0..627b475cf 100644 --- a/lib/generators/rspec/scaffold/templates/index_spec.rb +++ b/lib/generators/rspec/scaffold/templates/index_spec.rb @@ -18,7 +18,7 @@ it "renders a list of <%= ns_table_name %>" do render - cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td' + cell_selector = <%= Rails::VERSION::STRING >= '7' ? "'div>p'" : "'tr>td'" %> <% for attribute in output_attributes -%> assert_select cell_selector, text: Regexp.new(<%= value_for(attribute) %>.to_s), count: 2 <% end -%> diff --git a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb index edbc23934..4e02682e3 100644 --- a/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +++ b/spec/generators/rspec/scaffold/scaffold_generator_spec.rb @@ -268,6 +268,12 @@ .and(contain(/^RSpec.describe "(.*)\/index", #{type_metatag(:view)}/)) .and(contain(/assign\(:posts, /)) .and(contain(/it "renders a list of (.*)"/)) + + if ::Rails::VERSION::STRING >= '7.0.0' + expect(filename).to contain(/'div>p'/) + else + expect(filename).to contain(/'tr>td'/) + end end end