title: RSpec instance double expectations and default parameter values
body: <p>I'm trying to create a unit test for a class that has a dependency on an object I've created, <code>WebClient</code>, that makes authenticated web requests. I have created an <code>instance_double</code> of <code>WebClient</code> that I'm passing into the object and am going through mocking out its one public method, <code>make_json_request</code>, with a few sets of <code>with</code> expectations so it can return some hashes that I can use in my unit tests.</p>
  
  <p>In the first method I'm testing, the method is called like so:</p>
  
  <pre><code>@web_client.make_json_request '/api/v1/endpoint', query: { territory: territory_id, status: 'active' }
  </code></pre>
  
  <p>And I'm setting up my <code>instance_double</code> like so:</p>
  
  <pre><code>web_client = instance_double('WebClient')
  
  expect(web_client).to receive(:make_json_request)
     .with('/api/v1/endpoint', query: { territory: territory_id, status: 'active' })
     .and_return nil
  </code></pre>
  
  <p>When I attempt to run the tests in rspec, I get the following error:</p>
  
  <pre><code>Failure/Error: state = client.current_state
   #&lt;InstanceDouble(WebClient) (anonymous)&gt; received :make_json_request with unexpected arguments
     expected: ("/api/v1/endpoint", {:query=&gt;{:territory=&gt;1, :status=&gt;"active"}})
          got: ("/api/v1/endpoint", {:query=&gt;{}})
   Diff:
   @@ -1,2 +1,2 @@
   -["/api/v1/endpoint", {:query=&gt;{:territory=&gt;1, :status=&gt;"active"}}]
   +["/api/v1/endpoint", {:query=&gt;{}}]
  </code></pre>
  
  <p>It seems like it's receiving the message with the default values defined in the method signature, for some reason (method signature below):</p>
  
  <pre><code>def make_json_request(path, method: :get, body: {}, query: {})
  </code></pre>
  
  <p>I know for a fact that it's failing the expectation on the line that is actually passing the named parameter in with the appropriate values, and I'm at a loss of how to work around this. In other cases where I'm using a double, I have not had this problem - it's only in this one case, with named parameters with default values. What am I doing wrong?</p>
  
  <p><strong>Edit</strong>: I was asked for the full code of the method that is being tested:</p>
  
  <pre><code>def load_entities(territory_id)
      path = '/api/v1/endpoint'
      @web_client.make_json_request path, query: { territory: territory_id, status: 'active' }
      .select { |d| d['status'] == 'active' }
  end
  </code></pre>
  
  <p><strong>Edit 2</strong>: The full spec:</p>
  
  <pre><code>describe Loader do
    let(:territory_id) { 1 }
    let(:web_client) {
      web_client = instance_double('WebClient')
  
      expect(web_client).to receive(:make_json_request)
        .with('/api/v1/endpoint', query: { territory: territory_id, status: 'active' })
        .and_return nil
  
      web_client
    }
  
    describe '.current_state' do
      it 'returns a State' do
        client = Loader.new territory_id, web_client: web_client
  
        state = client.current_state
  
        expect(state).to be_a State
      end
  end
  </code></pre>