Skip to content

Commit

Permalink
Fix boolean interaction in scaffold system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcgibbon committed Dec 12, 2018
1 parent 0fc6804 commit ea89b45
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,7 @@
* Fix boolean interaction in scaffold system tests.

*Gannon McGibbon*

* Remove redundant suffixes on generated system tests.

*Gannon McGibbon*
Expand Down
Expand Up @@ -54,6 +54,11 @@ def attributes_hash
end
end.sort.to_h
end

def boolean?(name)
attribute = attributes.find { |attribute| attribute.name == name }
attribute&.type == :boolean
end
end
end
end
Expand Up @@ -16,8 +16,12 @@ class <%= class_name.pluralize %>Test < ApplicationSystemTestCase
click_on "New <%= class_name.titleize %>"

<%- attributes_hash.each do |attr, value| -%>
<%- if boolean?(attr) -%>
check "<%= attr.humanize %>" if <%= value %>
<%- else -%>
fill_in "<%= attr.humanize %>", with: <%= value %>
<%- end -%>
<%- end -%>
click_on "Create <%= human_name %>"

assert_text "<%= human_name %> was successfully created"
Expand All @@ -29,8 +33,12 @@ class <%= class_name.pluralize %>Test < ApplicationSystemTestCase
click_on "Edit", match: :first

<%- attributes_hash.each do |attr, value| -%>
<%- if boolean?(attr) -%>
check "<%= attr.humanize %>" if <%= value %>
<%- else -%>
fill_in "<%= attr.humanize %>", with: <%= value %>
<%- end -%>
<%- end -%>
click_on "Update <%= human_name %>"

assert_text "<%= human_name %> was successfully updated"
Expand Down
8 changes: 5 additions & 3 deletions railties/test/generators/scaffold_generator_test.rb
Expand Up @@ -5,7 +5,7 @@

class ScaffoldGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
arguments %w(product_line title:string product:belongs_to user:references)
arguments %w(product_line title:string approved:boolean product:belongs_to user:references)

setup :copy_routes

Expand All @@ -17,6 +17,7 @@ def test_scaffold_on_invoke
assert_file "test/models/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/
assert_file "test/fixtures/product_lines.yml"
assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/
assert_migration "db/migrate/create_product_lines.rb", /boolean :approved/
assert_migration "db/migrate/create_product_lines.rb", /references :user/

# Route
Expand Down Expand Up @@ -60,15 +61,16 @@ def test_scaffold_on_invoke

assert_file "test/controllers/product_lines_controller_test.rb" do |test|
assert_match(/class ProductLinesControllerTest < ActionDispatch::IntegrationTest/, test)
assert_match(/post product_lines_url, params: \{ product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test)
assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test)
assert_match(/post product_lines_url, params: \{ product_line: \{ approved: @product_line\.approved, product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test)
assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ approved: @product_line\.approved, product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test)
end

# System tests
assert_file "test/system/product_lines_test.rb" do |test|
assert_match(/class ProductLinesTest < ApplicationSystemTestCase/, test)
assert_match(/visit product_lines_url/, test)
assert_match(/fill_in "Title", with: @product_line\.title/, test)
assert_match(/check "Approved" if @product_line\.approved/, test)
assert_match(/assert_text "Product line was successfully updated"/, test)
end

Expand Down

0 comments on commit ea89b45

Please sign in to comment.