Search code examples
ruby-on-railsrspeccapybarapoltergeist

Mocking capybara page object - Rails / RSpec


I am working on a web crawler which accesses remote pages by submitting search queries via the remote site's search bar:

class Crawler < PoltergeistDriver

   def fetch_listing_page(query)
    ...
    visit 'whatever.com'
    fill_in('#seachbar', with: query)
    find('#searchbar').native.send_key(:return)
    ...
  end

end

I then use the page object to search for specific text inside the body like so:

class Crawler < PoltergeistDriver
  ...

  def fetch_listing_data
    ...
    page.all('some element', ...)
    ...
  end

  ...

end

My question is can I mock this page object in my tests? My guess is that VCR / Webmock would not work as the requests are made from the client side (poltergeist in my case).

EDIT:

AS Thomas suggested, I ended up injecting PoltergeistDriver instance as a dependency. This way I can simply create another driver (PuffingBilly) for tests and mock the return of page.body with static html.


Solution

  • You can't use WebMock/VCR for this, but you can use a programmable proxy. PuffingBilly - https://github.com/oesmith/puffing-billy - is one that integrates nicely with capybara and poltergeist.