0

What would be the best way to inject custom script tag into head/page for tests only?

I'm writing feature specs with capybara, and I need to mock some 3-rd party api calls in the browser, I need to inject javascript code.

I tried to page.execute_script, but it was too late. 3-rd party api calls are executed immediately on DOM ready. So I need to mock things before DOM ready.

I have some ideas, but I wonder what would be the best way?

Similar questions without answers:

Community
  • 1
  • 1
Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58

1 Answers1

1

Really the only way to do exactly what you're asking would be conditional code in your html templates, based on being in the test_environment (Rails.env.test?). However a much better solution is to use a programmable proxy like puffing_billy to mock the responses the 3-rd party api would actually send. That way you are still testing the code that would actually be used in production.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78