Reputation: 21019
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?
Upvotes: 2
Views: 1548
Reputation: 76218
That's because execute
is part of query
object and not the database itself.
https://github.com/mariano/node-db-mysql#quick-start
Upvotes: 2