Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I test this particular method?
    primarykey
    data
    text
    <p>I have the following method that is responsible for requesting a URL and returning it's <code>Nokogiri::HTML</code> document. This method checks if a proxy is defined and if it does, it will call <code>OpenURI</code>'s <code>open</code> with or without the proxy options.</p> <p><strong>Implementation</strong></p> <pre><code>require 'open-uri' require 'nokogiri' class MyClass attr_accessor :proxy # .... def self.page_content(url) if MyClass.proxy proxy_uri = URI.parse(MyClass.proxy) Nokogiri::HTML(open(url, :proxy =&gt; proxy_uri)) # open provided by OpenURI else Nokogiri::HTML(open(url)) # open provided by OpenURI end end end </code></pre> <p>I have no idea how I should write tests that prove the following:</p> <ol> <li>When a proxy is defined the request OpenURI makes actually uses the proxy info</li> <li>When a proxy isn't defined, a regular non-proxy connection is made</li> </ol> <p>Here's what I came up with as a start for the tests.</p> <pre><code>describe MyClass, :vcr do describe '.proxy' do it { should respond_to(:proxy) } end describe '.page_content' do let(:url) { "https://google.com/" } let(:page_content) { subject.page_content(url) } it 'returns a Nokogiri::HTML::Document' do page_content.should be_a(Nokogiri::HTML::Document) end # How do i test this method actually uses a proxy when it's set vs not set? context 'when using a proxy' do # ??? xit 'should set open-uri proxy properties' do end end context 'when not using a proxy' do # ??? xit 'should not set open-uri proxy properties' do end end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload