sinon

How do you mock MySQL (without an ORM) in Node.js?

♀尐吖头ヾ 提交于 2019-12-17 09:09:21
问题 I'm using Node.js with felixge's node-mysql client. I am not using an ORM. I'm testing with Vows and want to be able to mock my database, possibly using Sinon. Since I don't really have a DAL per se (aside from node-mysql ), I'm not really sure how to go about this. My models are mostly simple CRUD with a lot of getters. Any ideas on how to accomplish this? 回答1: With sinon, you can put a mock or stub around an entire module. For example, suppose the mysql module has a function query : var

Sinon.JS stub a function that resolves a promise

☆樱花仙子☆ 提交于 2019-12-14 03:22:15
问题 I want to use Sinon to stub a function that uses callbacks which resolve a promise: const callback = (err, data) => { if (err) { reject(err); } else { resolve(data); } }); stub.me({}, callback); I tried: var stub = { me: sinon.stub().yieldsTo("resolve", "my_data"), }; but I keep getting mocha timeouts. The code doesn't define a const for callback. It's all in the stub.me function call. I just wrote it like that so it would be clear to read. It's also wrapped in a new Promise((resolve,reject)

Mock internal dependency

 ̄綄美尐妖づ 提交于 2019-12-13 20:27:52
问题 I have a request that has an internal dependency to a Facebook graph objects that performs another request against the FB graph API. I'm wondering if it is possible to use sinon to mock the graph object so that it wouldn't actually perform a request in a test but would execute the callback function with a value that I provide in the test instead. server.post("/facebookLogin", function(req, res) { graph.setAccessToken(req.body.fbtoken); graph.get("me?fields=email", function(err, obj) { if (

How do you stub fetch api request

我怕爱的太早我们不能终老 提交于 2019-12-13 17:41:37
问题 I am having a problem with stubbing/testing function that is using fetch. Using simple example: export const clickTimeseries => { return fetch('...url...') .then(response => { if(response.status == 200) return response.json() else throw 'something' }) .then(json => { return { success: true, data: json, } }) .catch(err => { return { success: false, error: err, } }) } And my test: import { expect } from 'chai' import sinon from 'sinon' import Promise from 'es6-promise' Promise.polyfill() import

Test async function with mocha and sinon

风流意气都作罢 提交于 2019-12-13 08:52:13
问题 Background I am made a small function that emits messages via sockets and I am trying to test it using mocha and sinon: const myFun = socket => { socket.emit("first", "hello World!"); //some random amount of time passes socket.emit("second", "hello Moon!"); //other random amount of time passes socket.emit("third", "hello Mars? Venus? I dunno..."); }; Using sinon I can pass to my function a fakeSocket: const fakeSocket = { emit: sinon.spy() }; And check if I emit or not the messages. Problem

Testing a callback nested in a promise

一个人想着一个人 提交于 2019-12-13 08:01:56
问题 tl;dr I need to test that my method adds a row to a spreadsheet on successful load of a Google spreadsheet. saveDataToGoogleSpreadSheet(conversationData){ return new Promise((resolve, reject) => { Spreadsheet.load(this.getGoogleAPISettings(), (err, spreadsheet) => { if (err) { return reject(err); } return spreadsheet.receive((receivedError, rows, info) => { if (receivedError) { return reject(receivedError); } const rowData = this.getSpreadsheetRowData(conversationData, info.totalRows);

Unit testing with Supertest, Mocha & Sinon timing out

江枫思渺然 提交于 2019-12-13 07:29:54
问题 I am trying to write a unit/integration test where I want to get a list of things in the database. For not it is only a GET, but these tests needs to extend to POST, PUT & DELETE. The code I have thus far works fine, I can actually get data from the DB, but as soon as I try to stub out the function which is responsable for making the call to the DB, Mocha times out 1 failing 1) /account_types GET 200 List: Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this

How do I access this Javascript property?

*爱你&永不变心* 提交于 2019-12-13 04:25:31
问题 I need to make sure that a certain method inside the below shown UserMock -class was called. I've created this mock version to inject into another module to prevent default behaviour during testing. I am already using sinon.js , so how can I access a method such as isValid() and replace it with a spy/stub? Is it possible to do this without instantiating the class? var UserMock = (function() { var User; User = function() {}; User.prototype.isValid = function() {}; return User; })(); Thank you

Load jQuery into Mocha test for React App

我只是一个虾纸丫 提交于 2019-12-12 19:03:23
问题 I've created a Mocha test setup similar to this tutorial outlined here: https://github.com/jesstelford/react-testing-mocha-jsdom. I'm wondering how I can load in jQuery into this structure. I've included it as require('jquery') test JS file, but when I call $.ajax , it gives an error saying TypeError: Attempted to wrap undefined property ajax as function . I suspect that this is because Node is not running my JavaScript inside a browser. However, I've tried using a window environment, but

How to spy on an imported function using Sinon?

≡放荡痞女 提交于 2019-12-12 13:34:14
问题 Let's say we want to test that a specific function is called by another function using Sinon. fancyModule.js export const fancyFunc = () => { console.log('fancyFunc') } export default const fancyDefault = () => { console.log('fancyDefault') fancyFunc() } fancyModule.test.js import sinon from 'sinon' import fancyDefault, { fancyFunc } from '../fancyModule' describe('fancyModule', () => { it('calls fancyFunc', () => { const spy = sinon.spy(fancyFunc) fancyDefault() expect(spy.called).to.be.true