-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introduce acceptance testing #2830
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| --color | ||
| --require spec_helper | ||
| --exclude "**/black_box/*_spec.rb" |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| require "rails_helper" | ||
|
|
||
| RSpec.describe "with a Default Rails app" do | ||
| it "works" do | ||
| create_rails_application | ||
| setup_post_model | ||
| setup_administrate | ||
|
|
||
| app.start_and_wait_for_ready | ||
|
|
||
| visit("/admin") | ||
|
|
||
| expect(page).to have_css("header h1#page-title", text: "Posts") | ||
| click_on "New post" | ||
|
|
||
| fill_in "Body", with: "Some words!" | ||
| fill_in "Title", with: "A post with words" | ||
| click_button "Create Post" | ||
|
|
||
| expect(page).to have_content("Post was successfully created.") | ||
| expect(page).to have_content("Show Post #1") | ||
|
|
||
| app.stop | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something I didn't figure out yet: What if the test fails? We don't clean up the process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nickcharlton Is it possible to add a simple
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it might be. I started looking into the RSpec hooks to see what the most correct way to do this would be but I couldn't figure it out. The trouble with a simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one possible approach called |
||
| end | ||
|
|
||
| def session | ||
| @session ||= JetBlack::Session.new(options: {clean_bundler_env: true}) | ||
| end | ||
|
|
||
| def app | ||
| @app ||= TestAppProcess.new(session) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| module AcceptanceHelpers | ||
| def create_rails_application | ||
| session.create_file("Gemfile", <<~RUBY) | ||
| source "http://rubygems.org" | ||
|
|
||
| gem "rails" | ||
| RUBY | ||
|
|
||
| session.run("bundle install") | ||
|
|
||
| rails_new_cmd = [ | ||
| "bundle exec rails new .", | ||
| "--skip-test", | ||
| "--skip-spring", | ||
| "--force" | ||
| ].join(" ") | ||
|
|
||
| expect(session.run(rails_new_cmd)) | ||
| .to be_a_success.and have_stdout("force Gemfile") | ||
| end | ||
|
|
||
| def gem_path | ||
| File.expand_path(Rails.root + "../../") | ||
| end | ||
|
|
||
| def setup_administrate | ||
| session.append_to_file("Gemfile", "gem \"administrate\", path: \"#{gem_path}\"") | ||
| session.run("bundle install") | ||
|
|
||
| generate_dashboards_run = session.run( | ||
| "bundle exec rails g administrate:install" | ||
| ) | ||
|
|
||
| expect(generate_dashboards_run) | ||
| .to be_a_success.and have_stdout("create app/dashboards/post_dashboard.rb") | ||
| end | ||
|
|
||
| def setup_post_model | ||
| new_post_cmd = session.run([ | ||
| "bundle exec rails g model post", | ||
| "title:string", | ||
| "body:text", | ||
| "published_at:datetime" | ||
| ].join(" ")) | ||
|
|
||
| expect(new_post_cmd) | ||
| .to be_a_success.and have_stdout("create app/models/post.rb") | ||
|
|
||
| migrate_run = session.run("bundle exec rails db:migrate") | ||
|
|
||
| expect(migrate_run).to be_a_success.and have_stdout("CreatePosts: migrated") | ||
| end | ||
| end | ||
|
|
||
| RSpec.configure do |config| | ||
| config.include Capybara::DSL, type: :black_box | ||
| config.include Capybara::RSpecMatchers, type: :black_box | ||
| config.include AcceptanceHelpers, type: :black_box | ||
|
|
||
| config.before(:each, type: :black_box) do | ||
| Capybara.current_driver = :chrome | ||
| Capybara.run_server = false | ||
| Capybara.app_host = "http://localhost:3000" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| class TestAppProcess | ||
| def initialize(session) | ||
| @session = session | ||
| @process = | ||
| Bundler.with_unbundled_env do | ||
| SackRace::Process.new( | ||
| "bundle exec rails server", | ||
| { | ||
| chdir: session.directory, | ||
| verbose: true | ||
| } | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def start_and_wait_for_ready | ||
| Bundler.with_unbundled_env do | ||
| process.add_handler(:ready, "Use Ctrl-C to stop\n") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw this line in the sack_race readme and couldn't understand what it did. Now I see: it waits for the string to appear in stdout, triggering the given event. Perhaps add a line to that effect to the readme? 😄
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hah! Can you think of a better name? I might've gone a bit too generic when I was thinking it through.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm... Perhaps Or perhaps
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I think I'm a fan of Or, does it suggest that there could also be a
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think The word "handler" would be ok, except for the pairing with "wait for handler", which is not something I would normally expect. A handler handles, and I'm not sure it's something to wait for. I think to "wait for event" is more common.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yes, that's nice! Thanks! |
||
| process.start | ||
| process.wait_for_handler(:ready) | ||
| end | ||
| end | ||
|
|
||
| def stop | ||
| process.stop | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :process, :session | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before merging, I'll cut the full version of v1.0.0. It's at v1.0.0.rc1 (because it's been working well here so far).