Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, you need to arrange for the <code>proxy</code> method to return a proxy in one test case and not in the other. If there is a "setter" method for proxy, you can use that, otherwise you can stub the <code>proxy</code> method.</p> <p>Then, at a minimum, you want to set an expectation on <code>open</code> that it will be called with or without the <code>:proxy</code> option, depending on which test it is. Beyond that, you have the choice of whether to stub and set expectations for the various other calls involved in the method, including <code>URI.parse</code> and <code>Nokogiri::HTML</code>.</p> <p>See <a href="https://github.com/rspec/rspec-mocks" rel="nofollow noreferrer">https://github.com/rspec/rspec-mocks</a> for information on establishing your test doubles and setting expectations. Note in particular the <code>and_call_original</code> option if you want to use a partial stubbing approach.</p> <p>Update: Here's some code to get you started. This works for the non-proxy method. I've left the proxy case for you. Note also that this uses the "partial stubbing" approach, where you still end up calling the external gems.</p> <pre><code>require 'spec_helper' describe MyClass do describe '.proxy' do # NOTE: This test succeeds because of attr_accessor, but you're calling a MyClass.proxy (a class method) within your page_content method it { should respond_to(:proxy) } end describe '.page_content' do let(:url) { "https://google.com/" } let(:page_content) { MyClass.page_content(url) } # NOTE: Changed to invoke class method context 'when not using a proxy' do before {allow(MyClass).to receive(:proxy).and_return(false)} # Stubbed for no-proxy case it 'returns a Nokogiri::HTML::Document' do page_content.should be_a(Nokogiri::HTML::Document) end it 'should not set open-uri proxy properties' do expect(MyClass).to receive(:open).with(url).and_call_original # Stubbing open is tricky, see note afterwards page_content end 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 end end </code></pre> <p>Stubbing of <code>open</code> is tricky. See <a href="https://stackoverflow.com/questions/10796986/how-to-rspec-mock-open-uri/11258596#11258596">How to rspec mock open-uri?</a> for an explanation.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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