sinon

Does jasmine need sinon.js?

孤人 提交于 2019-12-03 09:40:58
I've seen examples on the web in which people use jasmine together with sinon . However, jasmine has support for spies (which as I understand is what Sinon does). So, the question is, is Sinon still useful when using Jasmine ? If Sinon is useful what exactly makes it a good addition to jasmine ? Cheers No you dont need Sinon to work with Jasmine. But Sinon spy/mock/stubs are more convenient in some cases. There is also a minor bug in mocking setTimeout in Jasmine, which work as expected with sinon. I use Sinon with Jasmine for it's fakeServer capabilities. Sinon allows me to easily mock AJAX

Sinon.Stub in Node with AWS-SDK

半世苍凉 提交于 2019-12-03 08:58:48
问题 I am trying to write some test coverage for an app that uses the aws-sdk NPM module that pushes things up to a SQS queue, but I am unsure how to mock things correctly. Here is my test so far: var request = require('superagent'), expect = require('chai').expect, assert = require('chai').assert, sinon = require('sinon'), AWS = require('aws-sdk'), app = require("../../../../app"); describe("Activities", function () { describe("POST /activities", function () { beforeEach(function(done) { sinon

Mocking/stubbing Mongoose model save method

女生的网名这么多〃 提交于 2019-12-03 07:17:06
问题 Given a simple Mongoose model: import mongoose, { Schema } from 'mongoose'; const PostSchema = Schema({ title: { type: String }, postDate: { type: Date, default: Date.now } }, { timestamps: true }); const Post = mongoose.model('Post', PostSchema); export default Post; I wish to test this model, but I'm hitting a few roadblocks. My current spec looks something like this (some stuff omitted for brevity): import mongoose from 'mongoose'; import { expect } from 'chai'; import { Post } from '../..

How to write test that mocks the $route object in vue components

℡╲_俬逩灬. 提交于 2019-12-03 05:41:49
问题 I have a component that contains statement like this.$route.fullPath , how should I mock value of fullPath of $route object if I want to test that component? 回答1: Best not mock vue-router but rather use it to render the component, that way you get a proper working router. Example: import Vue from 'vue' import VueRouter from 'vue-router' import totest from 'src/components/totest' describe('totest.vue', () => { it('should totest renders stuff', done => { Vue.use(VueRouter) const router = new

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

删除回忆录丶 提交于 2019-12-03 05:22:26
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.query("INSERT INTO USERS..."); }; return wrapper; })(); Cristiano Fontes You can use both for that. Mock

Sinon error Attempted to wrap function which is already wrapped

你说的曾经没有我的故事 提交于 2019-12-03 05:21:50
问题 Though there is a same question here but I could not find answer to my problem so here goes my question: I am testing my node js app using mocha and chai. I am using sinion to wrap my function. describe('App Functions', function(){ let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => { //some stuff }); it('get results',function(done) { testApp.someFun }); } describe('App Errors', function(){ let mockObj = sinon.stub(testApp, 'getObj', (dbUrl) => { //some stuff }); it('throws errors'

Can sinon stub withArgs match some but not all arguments

妖精的绣舞 提交于 2019-12-03 04:06:26
问题 I have a function I am stubbing that gets called with multiple arguments. I want to check just the first argument . The rest are callback function, so I want to leave them alone. Thus, I might have the following 2 calls, using ajax as an example: method.get = sinon.stub(); method.get(25,function(){/* success callback */},function(){/* error callback */}); method.get(10,function(){/* success callback */},function(){/* error callback */}); I cannot use method.get.calls... because then it doesn

Sinon JS “Attempted to wrap ajax which is already wrapped”

血红的双手。 提交于 2019-12-03 04:04:24
问题 I got the above error message when I ran my test. Below is my code (I'm using Backbone JS and Jasmine for testing). Does anyone know why this happens? $(function() { describe("Category", function() { beforeEach(function() { category = new Category; sinon.spy(jQuery, "ajax"); } it("should fetch notes", function() { category.set({code: 123}); category.fetchNotes(); expect(category.trigger).toHaveBeenCalled(); } }) } 回答1: You have to remove the spy after every test. Take a look at the example

How to mock a React component lifecycle method with Jest and Enzyme?

别说谁变了你拦得住时间么 提交于 2019-12-03 03:22:01
The Enzyme docs for Full DOM Rendering here contains the following example of spying on a lifecycle method with Sinon: describe('<Foo />', () => { it('calls componentDidMount', () => { sinon.spy(Foo.prototype, 'componentDidMount'); const wrapper = mount(<Foo />); expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true); }); }); What is the equivalent to this using mock functions from Jest? I'm using Create-React-App, and would rather not include Sinon if the same can be achieved with Jest. Here's what I'd expect the test to look like: describe('<App />', () => { it('calls

How to unit test console output with mocha on nodejs?

人走茶凉 提交于 2019-12-03 02:19:15
Take into account the following example Javascript code below: function privateFunction (time) { if (time < 12) { console.log('Good morning'); } if (time >= 12 && time <19) { console.log('Good afternoon'); } else { console.log('Good night!'); } }; How should I unit test that on nodejs using mocha (and possibly sinonjs), noticing that this is a private function called inside a module? I need to pass in the argument and check if the function is logging the right thing to the console. Can I do the same with console.warn and console.error ? I prefer mocha-sinon over "plain" sinon because it