2

I am trying to mock my database using Sinon with Mocha in Node.js for testing. I've tried the following:

var sinon = require('sinon');
var mysql = require('db-mysql');

beforeEach(function() {
  var db = sinon.mock(mysql);
  db.expects('execute');
});

but I keep getting the following error: TypeError: Attempted to wrap undefined property execute as function

I assumed that this is mocking the class and not the database instance. So I mocked the instance by doing var db = sinon.mock(new mysql.Database()); instead. When I did that, then all the valid methods for the db-mysql instance are passing, such as db.connect() and db.query(), no matter what the arguments are. I am unable to set behaviour. To set the behaviour, I am trying to call .expects on db but I get the following error:

TypeError: Object [object Object] has no method 'expects'

What's the right way to set the expected behaviour? Furthermore, how can I test several behaviours for the same function? Will I need to do this within each test according to what the test expects?

1 Answer 1

2

That's because execute is part of query object and not the database itself.

https://github.com/mariano/node-db-mysql#quick-start

Sign up to request clarification or add additional context in comments.

2 Comments

How do you stub out the query object in the database object, so you can run a stubbed out execute?
sinon.stub(db.query, "execute"); maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.