armature icon indicating copy to clipboard operation
armature copied to clipboard

Add TestSession

Open jgaskins opened this issue 10 months ago • 2 comments

Armature::TestSessions are similar to Capybara::Sessions in Ruby in that it acts like a fake web browser with sessions for your app.

You pass it your Armature::Route and you can interact with it through various expressive methods. For example, if my web app's entrypoint (the one I pass to HTTP::Server) is the Web class:

user = UserFactory.new.create password: BCrypt::Password.create("password", cost: 4)
session = Armature::TestSession.new(Web.new)

session.visit "/"
session.click_link "Login"
session.fill_in "input[name=email]", with: user.email
session.fill_in "input[name=password"], with: "password"
session.click_button "Login"

session.last_response.should have_status :ok
session.last_response.should have_html "Logged in as #{user.name}"

In addition to session.last_response, the visit, click_link, and click_button methods all return the response, so you can simply set response = session.click_button "Login" rather than repeating session.last_response over and over in the assertions.

It supports interactions via plain HTML links and forms as well as some HTMX attributes — I use HTMX for a lot of my own apps.

jgaskins avatar Jan 29 '25 16:01 jgaskins

I've been meaning to add this for almost a year and a half, ever since I wrote this post. I finally got around to extracting it from that app this week.

jgaskins avatar Jan 31 '25 23:01 jgaskins

It just occurred to me that this shard may not be the right place for this feature. It requires adding other shards as production dependencies that don't make sense.

I've been running into this issue with wax, too, and I ended up writing a separate wax-spec shard for apps to put into their development_dependencies instead. Following that pattern and making an armature-spec shard may make sense here, too. 🤔

jgaskins avatar Mar 31 '25 14:03 jgaskins