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

Automated Basic Tests #2414

Open
nhorton opened this issue Aug 8, 2023 · 1 comment
Open

Automated Basic Tests #2414

nhorton opened this issue Aug 8, 2023 · 1 comment

Comments

@nhorton
Copy link

nhorton commented Aug 8, 2023

Admin pages are often created en-masse and it is pretty easy to have changes in the main code inadvertently break admin pages.

I was about to start writing code to automatically do basic testing of the admin pages for our own system and realized that this was a common enough need that I should see if it existed. I can't find anything public for it, but figured that at Thoughtbot you might have some code like that already. Even if it is not tested enough to be in the gem, if you had anything to start from, that would be awesome. Otherwise, I will post what I end up with.

Thanks!

@nickcharlton
Copy link
Member

I've usually not bothered testing them (they are always the same, after all, and you'd expect model specs to pick up validations, etc) until they start taking on additional functionality or behaviour.

But I have always thought it'd be nice to auto-generate a set of basic tests that you can then extend or expand as it's always a pain to have to then backfill a load of tests!

Here's an example (that I've just renamed a few bits of) for updating:

require "rails_helper"

RSpec.describe "Updating printers" do
  scenario "as an admin" do
    create(:branch, name: "New Branch", city: "London")
    branch = create(:branch, name: "Original Branch")
    printer = create(:printer, branch: branch)
    admin = create(:admin)

    visit admin_printers_path(as: admin)
    click_on printer.branch_name
    click_on t("admin.printers.show.link.edit")
    fill_form_and_submit(
      :printer,
      :edit,
      branch: "London: New Branch",
      device_id: "new_id"
    )

    expect(page).to have_content(t("admin.printers.update.flashes.updated"))
    expect(page).not_to have_content("Original Branch")
    expect(page).to have_content("New Branch")
    expect(page).to have_content("new_id")
  end
end

And then deleting:

require "rails_helper"

RSpec.feature "Deleting printers" do
  scenario "by visiting the printers edit page", :js do
    admin = create(:admin)
    printer = create(:printer)

    visit admin_printers_path(as: admin)
    click_on printer.branch_name
    click_on edit_printer
    accept_confirm(confirmation_text) do
      click_on delete_button
    end

    expect(page).to have_content(destroyed_flash)
    expect(page).not_to have_content(printer.branch_name)
  end

  def edit_printer
    t("admin.printers.show.link.edit")
  end

  def delete_button
    t("admin.printers.edit.delete_button.text")
  end

  def confirmation_text
    t("admin.printers.edit.delete_button.confirm")
  end

  def destroyed_flash
    t("admin.printers.destroy.flashes.destroyed")
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants