sinon

Stubbing and/or mocking a class in sinon.js?

二次信任 提交于 2019-12-20 18:44:56
问题 I've created a database wrapper for my application, shown below. To test it, I obviously would like to replace the actual database library. I could create a new class that mocks the query method and catch all input there, but using sinon.js seems more appropriate, but how would I use it? Is the mock or stub features of sinon.js what I should be using? wrapper = (function() { function wrapper() {} wrapper.db = require("database"); wrapper.prototype.insertUser = function(doc) { return this.db

Understanding Sinon.js's yield(), yields(), and callsArg()

跟風遠走 提交于 2019-12-20 09:11:52
问题 What is the difference between stub.yield([arg1, arg2, ...]) spy.yields([arg1, arg2, ...]) stub.callsArg(index) in the Sinon.js stub library? stub.yield() is the only one that I've been able to grasp: stub = sinon.stub(API, 'call_remote'); callback = sinon.spy(); API.call_remote('help', callback); @stub.yield( "solution!" ); @stub.calledOnce.should.be.true; @callback.calledOnce.should.be.true; @callback.args[0][0].should.eql( "solution!" ); As tested with should.js, would have all assertions

node express es6 sinon stubbing middleware not working

﹥>﹥吖頭↗ 提交于 2019-12-19 03:36:33
问题 I am writing the mocha unit test for the my express router. I found that however I try to stub the middleware, it still execute the middleware code. Here is my router & test, could anyone figure out? Router: import { aMiddleware, bMiddleware, cMiddleware } from '../middleware.js'; router.post('/url', aMiddleware, bMiddleware, cMiddleware, function(req, res) { ... } Middleware: AuthMiddleware.aMiddleware = async (req, res, next) => { console.log('in real middleware'); next(); } Test: var

Creating request stub with sinon in mocha

試著忘記壹切 提交于 2019-12-18 18:42:08
问题 I'm using mocha to test some classes and I need to create a stub of request library. I'm using sinon , and I'm able to create a stub of the request.get method but I'm not able to create a stub of the request method (the http calls try to connect to a server). As I have read, request.get is an alias for request but when I stub request.get it has no effect over request calls. This code works (using request.get ): In tests: request = require 'request' describe "User test", -> user = {} before

Creating request stub with sinon in mocha

坚强是说给别人听的谎言 提交于 2019-12-18 18:42:03
问题 I'm using mocha to test some classes and I need to create a stub of request library. I'm using sinon , and I'm able to create a stub of the request.get method but I'm not able to create a stub of the request method (the http calls try to connect to a server). As I have read, request.get is an alias for request but when I stub request.get it has no effect over request calls. This code works (using request.get ): In tests: request = require 'request' describe "User test", -> user = {} before

Stubbing Redis interactions in javascript using Sinon

岁酱吖の 提交于 2019-12-18 03:33:09
问题 I am working in node.js. My app interacts with Redis via the node_redis module. I'm using mocha and sinon to automate testing of my app. My app looks something like this: ...snip var redisClient = redis.createClient(redisPort, redisHost); var someValue = redisClient.get("someKey"); return someValue; .... I want to stub the call to redisClient.get(). To do this I also need to stub the call to redis.createClient() - I think... Here's my test code: ... var redis = require("redis"); var

Sinon function stubbing: How to call “own” function inside module

耗尽温柔 提交于 2019-12-17 19:50:11
问题 I am writing some unit tests for node.js code and I use Sinon to stub function calls via var myFunction = sinon.stub(nodeModule, 'myFunction'); myFunction.returns('mock answer'); The nodeModule would look like this module.exports = { myFunction: myFunction, anotherF: anotherF } function myFunction() { } function anotherF() { myFunction(); } Mocking works obviously for use cases like nodeModule.myFunction() , but I am wondering how can I mock the myFunction() call inside anotherF() when called

Cleaning up sinon stubs easily

我怕爱的太早我们不能终老 提交于 2019-12-17 17:27:16
问题 Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this beforeEach -> sinon.stub some, 'method' sinon.stub some, 'mother' afterEach -> # I want to avoid these lines some.method.restore() some.other.restore() it 'should call a some method and not other', -> some.method() assert.called some.method 回答1: Sinon provides this functionality through the use of

How to mock middleware in Express to skip authentication for unit test?

丶灬走出姿态 提交于 2019-12-17 15:54:25
问题 I have the following in Express //index.js var service = require('./subscription.service'); var auth = require('../auth/auth.service'); var router = express.Router(); router.post('/sync', auth.isAuthenticated, service.synchronise); module.exports = router; I want to override or mock isAuthenticated to return this auth.isAuthenticated = function(req, res, next) { return next(); } Here is my unit test: it('it should return a 200 response', function(done) { //proxyquire here? request(app).post('

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

核能气质少年 提交于 2019-12-17 09:09:32
问题 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