sinon

Stubbing a class method with Sinon.js

安稳与你 提交于 2020-01-18 08:35:14
问题 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

Stubbing a class method with Sinon.js

空扰寡人 提交于 2020-01-18 08:32:08
问题 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

sinon spy is not called in a stub with async function

狂风中的少年 提交于 2020-01-14 10:34:06
问题 Using sinon and enzyme I wanna test the following component: // Apple.js class Apple extends Component { componentDidMount = () => { this.props.start(); Api.get() .then(data => { console.log(data); // THIS IS ALWAYS CALLED this.props.end(); }); } render () { return (<div></div>); } } If I just check endApy.called , it's always false. But wrapping it in a setTimeout will make it pass. Why console.log() is always called but not the props.end ? Why setTimeout fixes it? Is there a better way of

res.status not a function when trying to set the status code

元气小坏坏 提交于 2020-01-14 10:13:10
问题 When attempting to meet the specification set by unit tests supplied, when attempting to return the status code for a method I am hit with TypeError: res.status is not a function when running the function createUser in the API implementation. It happens with every method call, such as res.send, res.sendStatus etc. Even if I add res.status() to the testing to set it there, it returns the same error. apiTests.js let chai = require('chai'); let expect = chai.expect; let sinon = require('sinon');

How to use Fake timers with nightwatch.js and sinon.js?

女生的网名这么多〃 提交于 2020-01-13 16:29:15
问题 I'm doing JavaScript e2e test with nightwatch.js, and I want to mock the clock with sinon.js's fake timer http://sinonjs.org/docs/#clock But test stops before finish it, I got the log like below, and doesn't progress anymore. [some test] Test Suite =============================== ✔ Element <body> was visible after 5000 milliseconds. My test code is like below. How can I solve the problem? Thank you. module.exports = { before: function(browser) { clock = sinon.useFakeTimers(new Date(2015, 7,

Sequelize Model Unit Test

孤人 提交于 2020-01-13 11:22:55
问题 I have a User sequelize model that has a beforeCreate hook that encrypts the password using bcrypyt . Bcrypyt is loaded as a dependency by the model using a require statement. Now, I'm writing my tests for my models and I want to write a test that ensures bcrypt is hashing the password on create. At the moment, I have added a setter onto the User model that sets the bcrypt object. In my tests, I can then create a spy using sinon and inject the spy using the setter and ensure it is called on

How to get a sinon stub to call another function on nth call

天涯浪子 提交于 2020-01-07 05:46:27
问题 I want to use a sinon stub to asynchronously test an event emitter. I want the stub to call a callback after it is called. I thought stub.yields was what I want but not. Is there a neat way to do this? it('asynchronously emits finish after logging is complete', function(done){ const EE = require('events'); const testEmitter = new EE(); var cb = sinon.stub(); cb.calls(completed); // no such method but this is what I need testEmitter.on('finish', cb.bind(null)); testEmitter.emit('finish');

undefined|0|ReferenceError: Strict mode forbids implicit creation of global property 'csrf_token'

此生再无相见时 提交于 2020-01-07 04:41:06
问题 So, this was quite an interesting problem I have been running into. I am currently building a backbone.js - Rails app. Generally just building this for learning purposes. I am (like any good rails dev) doing my best at TDD/BDD and I ran into a problem with capybara. I have an integration spec that merely tests root_path works (Backbone history starts, displays initial information, etc...). require 'spec_helper' describe "RentalProperties", js: true do describe "GET /" do it "should show a

How to spy a function with Sinon.js that is in the same js file as the function under test

血红的双手。 提交于 2020-01-07 04:13:04
问题 I have a problem with Sinon.js while trying to spy a function that is in the same javascript file as the function that I want to test. Furthermore I assert that the spied function is called once. Unfortunately the test fails. Interesting thing is, that if the spied function is in another javascript file than the function under test it works ! Here is my code: mock_test.js: var sinon = require('sinon') var one = require('./one.js') var two = require('./two.js') describe('Spy ', function () {

How to stub oracledb with sinon?

让人想犯罪 __ 提交于 2020-01-06 07:19:14
问题 Here is my function which will return a promise once it gets data from oracle database: const getDataFromOracleDB = (filter, query) => new Promise(async (resolve, reject) => { let conn; try { conn = await oracledb.getConnection(dbConfig); const result = await conn.execute(query, [filter]); const { rows } = result; ... catch (err) { ... } }; As the unit test, I want to stub conn.execute , but have no idea how to do that. I've treid: const stub = sinon.stub(conn, 'execute').returns([1, 2, 3]);