vcr with capybara-webkit

后端 未结 2 2030
-上瘾入骨i
-上瘾入骨i 2021-02-04 08:06

I\'m using capybara-webkit to test integration with a third party website (I need javascript).

I want to use vcr to record requests made during the integration test but

相关标签:
2条回答
  • 2021-02-04 08:20

    Unfortunately, VCR is very much incompatible with capybara-webkit. The fact is that capybara webkit is using webkit, which is in c. Webmock and Fakeweb, which are the basis for VCR, can only be used for Ruby web requests. Making the two work together would likely be a monumental task.

    I've solved this problem two ways:

    The first (hacky, but valid) is to add a new javascript file to the application that is only included in the test environment. This file stubs out the JS classes which make external web requests. Aside from the pure hackatude of this approach, it requires that every time a new request is added or changed you must change the stubs as well.

    The second approach is to route all external requests through my own server, effectively proxying all external requests through my server. This has the huge disadvantage that you have to have an action for everything you want to consume (you could genericize it, with some work). It also suffers from the fact that it could as much as double the time for the request to complete. However, since the requests are now being made by Ruby you can use VCR in all it's glory.

    In my situations, approach #2 has been much more to my advantage thanks to the fact that I need ruby to manipulate the data so that I can keep my javascript source-agnostic. I was, however, using approach #1 for quite a while successfully.

    0 讨论(0)
  • 2021-02-04 08:35

    I've written a small ruby library (puffing-billy) for rspec+capybara that does exactly this -- it injects a proxy in between your browser and the outside world and allows you to fake responses to specific requests.

    Example:

    describe 'fetching badges from stackoverflow API' do
      it 'should show a nice message when you have no badges' do
        # stub some JSONP
        proxy.stub('http://api.stackoverflow.com/1.1/users/1/badges',
                   :jsonp => { :badges => [] })
    
        visit '/my_badges'
        page.should have_content("You don't have any badges :(")
      end
    end
    
    0 讨论(0)
提交回复
热议问题