I just want a simple mocked database that returns a value when a method is called, but i can't figure out how to do it. I am very new to javascript, so there might be something obvious that i am missing.
What i want is something close to this:
it("stub database", async () => {
var test = {code: 'test'}
var fake = sinon.stub(db, 'method')
fake.returns(test)
var result = fake.method("anything") // <- function does not exist
sinon.assert.match(result.code,"test")
});
But this does not work.
This almost works
it("stub database", async () => {
var test = {code: 'test'}
sinon.stub(db, 'method').returns(test)
var result = db.method("anything")
sinon.assert.match(result.code,"test")
});
But if i then send the db to another instance, the stub stops working.
The db instance i want to replace is
module.exports = exports = (app, data = null) => {
data = data(app) || require('db')(app)
exports.app = app
return exports
}
exports.method = (input) => {
return data.method(input)
}
I feel like this is the easiest thing ever in any other language, but i have no idea why this is so hard in node, so any help would be very helpful