sinon

How to stub a method that is called from an outer scope to the function under test?

谁都会走 提交于 2019-12-12 11:29:41
问题 I have a Redis client that is created thus using the node_redis library (https://github.com/NodeRedis/node_redis): var client = require('redis').createClient(6379, 'localhost'); I have a method I want to test whose purpose is to set and publish a value to Redis, so I want to test to ensure the set and publish methods are called or not called according to my expectations. The tricky thing is I want this test to work without needing to fire up an instance of a Redis server, so I can't just

Sinon - How to stub authentication library (Authy -Twilio)

懵懂的女人 提交于 2019-12-12 03:42:34
问题 I am currently new to Sinon, Mocha, Supertest and in the process to writes tests. In my current scenario, i have authentication library which verifies my "OTP" and after verifying it proceeds to do operation within the callback function. I am unable to mock the callback to return null and carry on to test rest of the code. Following is my code snippet: Controller.js var authy = require('authy')(sails.config.authy.token); authy.verify(req.param('aid'), req.param('oid'), function(err, response)

angular js karma/chai - mock an authorization error

雨燕双飞 提交于 2019-12-12 03:38:46
问题 I am new to TDD and am testing an authInterceptor (I have chai/mocha/sinon available to me) in angular js, which has two functions, a request, and a responseError. I successfully tested the request function, but I don't know how (scoured the docs) to mock a 401 (unauthorized) error. Here is the interceptor: export default function AuthInterceptor($q, $injector, $log) { 'ngInject'; return { request(config) { let AuthService = $injector.get('AuthService'); if (!config.bypassAuthorizationHeader)

QUnit/Sinon: testing a function which checks if cookies are enabled

蹲街弑〆低调 提交于 2019-12-12 02:07:28
问题 I have the following (simplified) javascript module which uses jQuery Cookie plugin to check if cookies are enabled. If cookies are disabled it warns the user: var cookiePolicy = (function () { var cookiesEnabled = function () { return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid'; }; return { updateCookiePolicy: function () { if (!cookiesEnabled()) { $("#cookie-policy").append('<p id="cookie-warning">Cookies are disabled. Some features of this site may not work

Sinon Spy not working with Javascript call or apply

眉间皱痕 提交于 2019-12-12 01:47:40
问题 I'm trying to use sinon.spy to check if the play function is being called for a component. The problem is that the spy counter is not updating, even though I've confirmed that my component's function is indeed being called. I've tracked it down to the use of Javascript's call function: handleDone: function(e) { for (var i = 0; i < this.components.length; i++) { if (this.components[i].element === e.target) { if (this.components[i].hasOwnProperty("play")) { // This won't trigger the spy. this

Can ES6 constructors be stubbed more easily with Sinon?

丶灬走出姿态 提交于 2019-12-11 18:37:15
问题 Given the (overly simplified) snippet: import Validator from 'validator'; export default function isValid(arg) { // Validator#isValid is an ES6 getter return new Validator(arg).isValid; } How can I test that a Validator was instantiated with the given parameter? And stub isValid ? I know I can restructure my code to avoid the issue, I am not looking for a workaround as I found plenty (dependency injection, not using ES6 sugar, etc.). I found a way, but it is terribly ugly. In test file:

Sending Http2 Request to GRPC Server Node, Illegal Buffer

狂风中的少年 提交于 2019-12-11 16:17:09
问题 I am attempting to utilize protobuf.js and provide it a transport layer (rpcimpl) since it is transport agnostic. I can successfully convert all the proto files and to a direct grpc Client and Sever via protobuf (loadSync, lookup) to grpc's (loadObject). This allows me to get a concrete grpc implementation of server and client up with tests. The next step was to test a protobuf client (instable) to grpc server stable. This is more out of curiosity to see if we can be independent of grpc's

How to call $(document).ready(function() {…}); from another file JS

霸气de小男生 提交于 2019-12-11 14:05:10
问题 I would like to know how to call the function below without refactoring from another js file. $(document).ready(function() { check(); function check () { setTimeout(function(){ location.reload(true); }, 10000); } }); I saw a question exist for this issue very old one but I cannot understand how to use the answer for my function. StackOverflow answer from another question I would like to see an example with my function and the proposed solution from the link. My example which does not work

Simple way to test middleware in Express without creating recreating server?

独自空忆成欢 提交于 2019-12-11 13:02:04
问题 I'd like to be able to stub my middleware functions on a per-test basis. The problem, as articulated here, is that I can't just stub my middleware functions since node has cached the middleware function so I can't stub it since I create my app at the very beginning. const request = require("supertest"); const { expect } = require("chai"); const sinon = require('sinon'); const auth = require ("../utils/auth-middleware") const adminStub = sinon.stub(auth, "isAdmin").callsFake((req, res, next) =

sinon stub with es6-promisified object

Deadly 提交于 2019-12-11 11:43:39
问题 Ok my setup is as follows: Using node 6.2, es6-promisify, sinon, sinon-as-promised, and babel to transpile support for es6 import/export. My code under test looks something like this: const client = restify.createJsonClient({ url: 'http://www.example.com' }); export let get = promisify(client.get, {thisArg: client, multiArgs: true}); export default function* () { yield get('/some/path'); } And then in my test file I have something like this: import * as m from mymodule; it('should fail',