thoughtbot / administrate

A Rails engine that helps you put together a super-flexible admin dashboard.
http://administrate-demo.herokuapp.com
MIT License
5.86k stars 1.11k forks source link

Automated Basic Tests #2414

Open nhorton opened 12 months ago

nhorton commented 12 months ago

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 commented 12 months ago

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