← Back to Upcase

Using Capybara Outside of Testing Env with a Fake


(Anthony Ross) #1

I question if I’m using Capybara outside the scope of it’s design, though I’ve come this far so I thought I’d ask. At the highest level I need my application in production to:

  • go to a few external links and fill in forms for the user

As an example, a form has:

  • First Name
  • Age

I’ve got a Sidekiq worker that fills in that information like so:

session = Capybara::Session.new(:selenium)
session.visit(third_party_url)
session.fill_in "First Name", with: name
session.fill_in "Age", with: age
session.click_on "Submit"

This works just as I need it to. The problem is I have not found a way to write a test for this, though I’ve tried endlessly.

I have faked the third_party_url following this awesome guide here. The problem is, session.visit(third_party_url) isn’t triggering a get request to my fake, it’s skipping right over it. What I don’t understand is if I replace session.visit(third_party_url) with a Net:HTTP.get(third_party_url) I get right to my fake. I’ve looked at the capybara source and it looks like it’s just making a simple get request here

Currently my test looks like this:

require 'rails_helper'

describe FormWorker do
  it 'fills in a form' do
    SyncWorker.new.perform("Tom", "18")

    expect(user.forms_filled.count).to eq 1
  end
end

Instead what happens is I run the test and Firefox pops up with a 422 error. I’m looking for feedback both on my implementation and testing strategies and really open to any suggestions. Thank you!


(Ben Orenstein) #2

Glancing at this quickly, I wonder if you want something like https://github.com/thoughtbot/capybara_discoball (we use this on many of our apps).

If you’re on our Aspiring Professional plan, you can see us using it in the Upcase app here: https://github.com/thoughtbot/upcase/blob/master/spec/support/fake_stripe.rb