sinon

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

匆匆过客 提交于 2019-11-27 11:54:06
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? With sinon, you can put a mock or stub around an entire module. For example, suppose the mysql module has a function query : var mock; mock = sinon.mock(require('mysql')) mock.expects('query').with(queryString, queryParams).yields(null,

How to test an ES6 class that needs jquery?

给你一囗甜甜゛ 提交于 2019-11-27 05:39:16
I have an ES6 module that needs jquery. import $ from 'jquery'; export class Weather { /** * Constructor for Weather class * * @param latitude * @param longitude */ constructor(latitude, longitude) { this.latitude = latitude; this.longitude = longitude; } /** * Fetches the weather using API */ getWeather() { return $.ajax({ url: 'http://localhost:8080/weather?lat=' + this.latitude + '&lon=' + this.longitude, method: "GET", }).promise(); } } Module works fine when use it in my main module but the issue is with the test that I am writing for it. Here's the test: import {Weather} from '../js

How to properly unit test jQuery's .ajax() promises using Jasmine and/or Sinon?

巧了我就是萌 提交于 2019-11-27 05:02:48
问题 I've got a fairly straightforward function which returns a jQuery .ajax() promise as such: CLAW.controls.validateLocation = function(val, $inputEl) { return $.ajax({ url: locationServiceUrl + 'ValidateLocation/', data: { 'locationName': val }, beforeSend: function() { $inputEl.addClass('busy'); } }).done(function(result) { // some success clauses }).fail(function(result) { // some failure clauses }).always(function() { // some always clauses }); } For the most part, this new promises

Sinon stub function used with destructuring

橙三吉。 提交于 2019-11-27 02:54:13
问题 I wish to stub a function used in the file I'm currently testing. This function is required with a destructuring like this: const { theFunctionIWant } = require('path/to/module') When testing, the stub is never called, and the real function proceed to be called. But when I require it 'normally' (i.e: without destructuring) const myModule = require('path/to/module') then the stub is correctly used and everything works fine I sense that it's because of how the destructuring works and the fact

How to mock localStorage in JavaScript unit tests?

▼魔方 西西 提交于 2019-11-27 00:33:27
Are there any libraries out there to mock localStorage ? I've been using Sinon.JS for most of my other javascript mocking and have found it is really great. My initial testing shows that localStorage refuses to be assignable in firefox (sadface) so I'll probably need some sort of hack around this :/ My options as of now (as I see) are as follows: Create wrapping functions that all my code uses and mock those Create some sort of (might be complicated) state management (snapshot localStorage before test, in cleanup restore snapshot) for localStorage. ?????? What do you think of these approaches

How does one stub promise with sinon?

风流意气都作罢 提交于 2019-11-27 00:26:15
问题 I have a data service with following function function getInsureds(searchCriteria) { var deferred = $q.defer(); insuredsSearch.get(searchCriteria, function (insureds) { deferred.resolve(insureds); }, function (response) { deferred.reject(response); }); return deferred.promise; } I want to test following function: function search () { dataService .getInsureds(vm.searchCriteria) .then(function (response) { vm.searchCompleted = true; if (response.insureds.length > 100) { vm.searchResults =

Stubbing a class method with Sinon.js

雨燕双飞 提交于 2019-11-26 23:55:22
I am trying to stub a method using sinon.js but I get the following error: Uncaught TypeError: Attempted to wrap undefined property sample_pressure as function I also went to this question ( Stubbing and/or mocking a class in sinon.js? ) and copied and pasted the code but I get the same error. Here is my code: Sensor = (function() { // A simple Sensor class // Constructor function Sensor(pressure) { this.pressure = pressure; } Sensor.prototype.sample_pressure = function() { return this.pressure; }; return Sensor; })(); // Doesn't work var stub_sens = sinon.stub(Sensor, "sample_pressure")

How to Unit Test React-Redux Connected Components?

纵饮孤独 提交于 2019-11-26 22:03:47
I am using Mocha, Chai, Karma, Sinon, Webpack for Unit tests. I followed this link to configure my testing environment for React-Redux Code. https://medium.com/@scbarrus/how-to-get-test-coverage-on-react-with-karma-babel-and-webpack-c9273d805063#.7kcckz73r I can successfully test my action and reducers javascript code, but when it comes to testing my components it always throw some error. import React from 'react'; import TestUtils from 'react/lib/ReactTestUtils'; //I like using the Test Utils, but you can just use the DOM API instead. import chai from 'chai'; // import sinon from 'sinon';

Sinon stub being skipped as node express middleware

五迷三道 提交于 2019-11-26 21:51:42
问题 I'm trying to test the behavior of a particular route. It continues to run the middleware even when I create a stub. I want the event authentication to simply pass for now. I understand that it's not truly a "unit" test at this point. I'm getting there. I've also simplified the code a little. Here is the code to test: const { rejectUnauthenticated } = require('../modules/event-authentication.middleware'); router.get('/event', rejectUnauthenticated, (req, res) => { res.sendStatus(200); });

How to mock localStorage in JavaScript unit tests?

六月ゝ 毕业季﹏ 提交于 2019-11-26 17:27:43
问题 Are there any libraries out there to mock localStorage ? I've been using Sinon.JS for most of my other javascript mocking and have found it is really great. My initial testing shows that localStorage refuses to be assignable in firefox (sadface) so I'll probably need some sort of hack around this :/ My options as of now (as I see) are as follows: Create wrapping functions that all my code uses and mock those Create some sort of (might be complicated) state management (snapshot localStorage