65

Testing the multi-subdomain Rails app

 5 years ago
source link: https://www.tuicool.com/articles/hit/fyiy2iq
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Writing tests for a multi-tenant , multi-subdomain app turns out to be very tricky to figure out, e.g.:

VFzYRbm.png!web

I saw that, and understood the frustration. Integration tests (“ request specs ” or “ feature specs “) are built on a stack of frequently changing libraries and shifting API’s. And the recipe for subdomain-aware testing isn’t documented in any particular tool’s notes. So here’s my working configuration for Rails 5.2.0, in mid-2018.

The goal: feature- and request-specs to specific hostnames

I’m creating a set of sites with laws for each state:

texas.public.law , newyork.public.law , etc.

And there’s one “organizational home site”:

www.public.law

…with pages like About and Contact Us. I want to write request specs like these:

...and feature specs like this:

Spec helpers: get_path and visit_path

I’ve written two helpers, seen in the examples above: get_path for request specs, and visit_path for feature specs. get_path turned out to need very little code. However, it was difficult to figure out because it required reaching through RSpec, the Rails “integration” test support (to get the headers: parameter) and Rails APIs to determine that host: is the correct header to supply:

The equivalent code for feature specs, which are based on Capybara, requires a bit more work:

I simply place these helpers in my rails_helper.rb file.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK