Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems on chaining count with where in array plus limit #35

Open
kolbma opened this issue Jun 26, 2018 · 5 comments
Open

Problems on chaining count with where in array plus limit #35

kolbma opened this issue Jun 26, 2018 · 5 comments

Comments

@kolbma
Copy link

kolbma commented Jun 26, 2018

How do I chain this?

.count({ x: { $in: ['a','b'] }, u }).limit(1).exec()

I've tried

mock.expects('count')
      .chain('limit')
      .chain('exec')
      .resolves(1);

Result:

    ExpectationError: Unexpected call: count({
      x: { $in: ['a','b'] },
      u: 012345678901234567891234
    })
        Expectation met: count([...]) once

I've also tried different chain orders.
I've also tried with

.count({ u }).where('x').in(['a','b']).limit(1).exec()

chaining also where and in and again different order combinations.
Doesn't work.

@gaguirre
Copy link
Collaborator

Hi @kolbma, could you post the complete example? (including test and the code being tested)

@ollyjohn
Copy link

ollyjohn commented Jul 5, 2018

@gaguirre,
I'm facing a similar issue when the database call I'm mocking would be called more than once during the test.

//service-under-test.js
function updateClients(logger, tenant, user, clients) {
   ...
   // check if the client exists first...
   clients.forEach( ( client) => {
      DbCollection.findOne({tenant: tenant, clientID: client.clientID}).lean().then((result)=> {...});
   })
   ...
}

module.exports = { updateClients: updateClients };
//service-under-test.test.js
it('should not error just because of multiple calls', () => {
   sinon.mock(DbCollection).expects('findOne').chain('lean').resolves(someVal);

   service.updateClients(logger, 'uk-en', user, [{clientID; 'a', newVal: ...}, {clientID: 'b', newVal: ...}];
   ...
});

On running this test, I get the following error:

Unhandled rejection ExpectationError: Unexpected call: findOne({ clientID: "a", tenant: 'uk-en'})
    Expectation met: findOne([...]) once

Hope that helps?

@gaguirre
Copy link
Collaborator

gaguirre commented Jul 5, 2018

What version of sinon are you using? @kolbma @ollyjohn

@ollyjohn
Copy link

@gaguirre - I'm on the latest version - ^6.1.0

@natanavra
Copy link

natanavra commented May 16, 2019

Just ran into met once errors myself, could be you guys are doing similar mongoose queries somewhere else in your tested code? The mock is expected to run only once...
The solution for me was adding to each chain and expects a twice() or more function:

sandbox.mock(Notification)
	.expects('findOne').twice()
	.chain('cache').twice()
	.chain('exec').twice()
	.resolves({});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants