Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system tests to generate scaffold #29418

Merged
merged 1 commit into from Jun 30, 2017

Conversation

dinahshi
Copy link
Contributor

@dinahshi dinahshi commented Jun 11, 2017

generate scaffold currently generates a simple system test. This change
populates the generated system test with index, create, update, and destroy
behaviour. Feature request from @eileencodes

Sample generated test from bin/rails generate scaffold HighScore game:string score:integer:

require "application_system_test_case"

class HighScoresTest < ApplicationSystemTestCase
  setup do
    @high_score = high_scores(:one)
  end

  test "visiting the index" do
    visit high_scores_url
    assert_selector "h1", text: "High Scores"
  end

  test "creating a High score" do
    visit high_scores_url
    click_on "New High Score"

    fill_in "Game", with: @high_score.game
    fill_in "Score", with: @high_score.score
    click_on "Create High score"

    assert_text "High score was successfully created"
    click_on "Back"
  end

  test "updating a High score" do
    visit high_scores_url
    click_on "Edit", match: :first

    fill_in "Game", with: @high_score.game
    fill_in "Score", with: @high_score.score
    click_on "Update High score"

    assert_text "High score was successfully updated"
    click_on "Back"
  end

  test "destroying a High score" do
    visit high_scores_url
    page.accept_confirm do
      click_on "Destroy", match: :first
    end

    assert_text "High score was successfully destroyed"
  end
end

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really great! I just tried it out and all the tests worked with the scaffold! 😄

I left a few comments for things that need some changes. Thanks for working on this!


unless options.api?
template "system_test.rb", File.join("test/system", class_path, "#{file_name.pluralize}_test.rb")
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to check depends_on_system_test? because some users may want to skip system tests when generating the scaffold.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this method is defined in Rails::Generators::AppBase. Would it be best to replicate this in TestUnit::Generators::Base or rely on the system_tests option set here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you wrote is correct. I was thinking of the skip code elsewhere in the generators.

["#{name}", "@#{singular_table_name}.#{name}"]
end
end.sort.to_h
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rubocop build is failing for these because the methods should be indented 2 spaces under private and there should be a space after the bracket here {|k, v| "#{k}: #{v}"}.join(", ").

@@ -29,7 +29,7 @@ class <%= controller_class_name %>ControllerTest < ActionDispatch::IntegrationTe
end

test "should update <%= singular_table_name %>" do
patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_hash} }" %> }, as: :json
patch <%= show_helper %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did these need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes_hash formerly referred to a string representation of the attributes (i.e. "{key: value}"). I needed a real ruby hash to iterate over so I named that method attributes_hash and renamed this to reflect its type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool thanks for the explanation!

@@ -16,13 +16,10 @@ class ScaffoldGenerator < ResourceGenerator # :nodoc:
def handle_skip
@options = @options.merge(stylesheets: false) unless options[:assets]
@options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] && options[:scaffold_stylesheet]
@options = @options.merge(system_tests: false) if options[:api]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I swapped this out for @options = @options.merge(system_tests: nil) if options[:api] and it still wasn't available in the options hash in railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb so I left the options.api? check in. I'm not sure what the purpose of this line is.

@eileencodes
Copy link
Member

Awesome work @dinahshi! Can you squash your commits into 1 and then I'll merge? Thanks so much for working on this.

Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one issue when we're skipping system tests on the scaffold generator. There's a missing test case since all the tests are passing.


unless options.api? || options[:system_tests].nil?
template "system_test.rb", File.join("test/system", class_path, "#{file_name.pluralize}_test.rb")
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested this and there's a missing test case. This doesn't work for generating a scaffold and passing the skip system test option.

bin/rails g scaffold Tree title:string --skip-system-test

In that case the system test file would still be generated because we're not checking at all for the skip-system-test option which is why I ended up changing the config file in the other PR. Because then otherwise we end up checking for 2 options for skipping system tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this on master and it's not currently supported. bin/rails g scaffold Tree title:string --skip-test also doesn't skip any tests (including model, controller, and system tests). As a result I think adding command line options to non-application generate commands is likely a larger task and should be separate from this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it's not supported on master. I think this PR is fine then and if we want to implement --skip-system-test for scaffold we can do that in a separate PR.

@eileencodes eileencodes merged commit 6671340 into rails:master Jun 30, 2017
@dinahshi dinahshi deleted the scaffold_system_tests branch January 24, 2018 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants