Is there a way to write a Minitest "DSL" module for a Capybara session (similar to rails-dom-testing)

Viewed 8

For integration tests, the Rails docs suggest an intriguing pattern for writing "DSL" modules that can "extend [session] instances with assertion methods" for a pattern of assertions specific to certain types of sessions.

(...ok, seems like a stretch to use the term "DSL" here, but that's the Rails docs.)

Their example, using rails-dom-testing sessions, is something like this:

require("test_helper")

class StudentTest < IntegrationTestCase

  def test_creating_drafts
    rolf_session    = open_session.extend(AdminDsl)
    mary_session    = open_session.extend(CreatorDsl)
    ...open some other sessions

    rolf_session.login!(rolf)
    mary_session.login!(mary)

    url = mary_session.create_paper(name, gen_desc, project)
    rolf_session.check_admin(url, gen_desc, project)
    ... keep switching between sessions and do stuff
  end

  module AdminDsl
    def check_admin(url, gen_desc, project)
      get(url)
      ...accomplish some admin stuff, clicks, and assertions
    end
  end

  module CreatorDsl
    def create_paper(name, gen_desc, project)
      ...create a paper, save it, assert things
    end 
  end

  ...other modules maybe
end

I know how to switch sessions in Capybara, it's well documented.

But is there a way to extend Capybara sessions with these "DSL"-type modules, similar to the above? When I try it, making necessary adjustments for Capybara syntax, i'm getting eval error: uninitialized constant Query::Base; the methods declared in the module seem not to work within the type of object that is a Capybara Session.

I'm wondering if it's even possible. Or advisable!

0 Answers
Related