title: How to mock method which returns different values depending on the received arguments?
body: <p>I have a method that returns different values depending on the received arguments</p>
  
  <pre class="lang-js prettyprint-override"><code>const myAPI = {
      getRepository: async data =&gt; await connectDB(data)
  }
  
  userService = {
      create: async data =&gt; {
          const user = await getRepository('User').save(data.user)
          const image = await getRepository('Image').save(data.image)
  
          // this code shows that returned values matter
          if (user &amp; image) {
              return { user, image }
          } else if (user) {
              return { user }
          } else if (image) {
              return { image }
          } else {
              return {}
          }
      }
  }
  </code></pre>
  
  <p>Here is my mock</p>
  
  <pre class="lang-js prettyprint-override"><code>import { createSandbox } from 'sinon'
  
  const mock = createSandbox().mock(myAPI)
  mock
    .expects('getRepository')
    .withArgs('User')
    .returns(fakeUser)
    .withArgs('Image')
    .returns(fakeImage)
  
  (async () =&gt; await userService.create({ user, image }))()
  
  </code></pre>
  
  <p>And I have a following error</p>
  
  <pre><code>ExpectationError: Unexpected call: getRepository(function User() {})
  Expected getRepository([function Image() {}, function User() {}][, ...]) once (never called)
  </code></pre>
  
  <p>How should my code looks so that the mock works properly?</p>