logo
down
shadow

Sinon stub on prototype, check value of actual instance it is called on


Sinon stub on prototype, check value of actual instance it is called on

By : Merle
Date : November 22 2020, 04:01 AM
This might help you Yes, this is possible.
You can use sinon.assert.calledOn, spy.calledOn, spyCall.thisValue, or spy.thisValues to check the this value for calls:
code :
import * as sinon from 'sinon';

class MyType {
  constructor(id) {
    this.identifier = id;
  }
  someFunction(arg) { }
}

test('someFunction', () => {
  const stub = sinon.stub(MyType.prototype, 'someFunction');

  const one = new MyType("oneId");
  const two = new MyType("twoId");

  one.someFunction('firstArg');
  two.someFunction('secondArg');

  sinon.assert.calledWith(stub.firstCall, 'firstArg');  // SUCCESS
  sinon.assert.calledOn(stub.firstCall, one);  // SUCCESS
  expect(stub.firstCall.thisValue.identifier).to.equal('oneId');  // SUCCESS

  sinon.assert.calledWith(stub.secondCall, 'secondArg');  // SUCCESS
  sinon.assert.calledOn(stub.secondCall, two);  // SUCCESS
  expect(stub.secondCall.thisValue.identifier).to.equal('twoId');  // SUCCESS
});


Share : facebook icon twitter icon
When using Sinon, how to replace stub function in a stub instance?

When using Sinon, how to replace stub function in a stub instance?


By : Paula
Date : March 29 2020, 07:55 AM
I wish this help you I don't think this does make a lot of sense, because if you're replacing a stub with another stub... then why don't you just use the first stub to achieve whatever you want to do with the second stub.
Anyways, the sinon.stub(object, "method", func) does the following according to the docs:
code :
var a = sinon.createStubInstance(MyConstructor);
a.method = sinon.spy(function() { return "Foo Bar"; });
var a = sinon.createStubInstance(MyConstructor);
a.method = sinon.stub();
Sinon.stub getting called in test, but expect to be called statement fails

Sinon.stub getting called in test, but expect to be called statement fails


By : cinculator
Date : November 04 2020, 04:00 PM
seems to work fine Well, this had to do with the .bind in the onClick call. The solution was to organize the props to accept a sinon stub, but in all other, production cases, to call the _handleTabChange function by default.
This meant that handleTabChange had to return a function.
Sinon js check stub called with exact arguments

Sinon js check stub called with exact arguments


By : Kevin Wojcik
Date : March 29 2020, 07:55 AM
I hope this helps . Sinon js check stub called with exact arguments , Here are 2 problems with this line:
code :
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), data, cb_2);
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), {data: data});
sinon deny a stub being called even though it was called

sinon deny a stub being called even though it was called


By : user2494746
Date : March 29 2020, 07:55 AM
wish helps you It seems that expect(res.status.calledOnce).to.equal(true) was called immediately before waiting for sinon.assert.calledWith.
putting await before sinon.assert fixed the problem.
code :
  it('should return status 500 on server error', async () => {
      sinon.stub(ActivityService, 'findActivity').rejects()
      await ActivityController.findActivity(req, res)
      await sinon.assert.calledWith(ActivityService.findActivity, req.params.id, req.user)
      expect(res.status.calledOnce).to.equal(true)
      sinon.assert.calledWith(res.status, 500)
    })
Sinon js AssertError: expected stub to be called once but was called 0 times

Sinon js AssertError: expected stub to be called once but was called 0 times


By : mspurge1
Date : March 29 2020, 07:55 AM
hop of those help? simpleMethod is an instance method. To stub an instance method, you should stub the prototype.
Try this in your code.
code :
myStub = sinon.stub(MyLib.prototype, 'simpleMethod');
myStub.restore();
Related Posts Related Posts :
  • Microservices with Spring boot, Message queue, Node.js and Angular
  • Jest js Install - getting error -- npm ERR! Class constructor cannot be invoked without 'new'
  • Node.js command-line variable not recognized
  • Score document by array aggregation
  • Post Request from NodeJS results in empty package
  • Serverside NodeJS - need client windows ID
  • Maximum Text Size For createBlockBlobFromText method Azure Storage, NodeJS
  • How to say news in Alexa with Node.js
  • Find when the keys are unkown in mongodb
  • Service not found in Feathersjs
  • Dockerized Loopback 4 app not reachable from the host
  • node loopback updateAll update property with another property value
  • How to check if all values exists inside nested object elastic search
  • Heroku git push error no matching version for Node: 6.4.1
  • Express async returns erly
  • How to write in a temp directory in Lambda in the same invocation?
  • Firebase storage : list files in a specific directory
  • AdonisJS single route won't open
  • Generate millions of mock data using nodejs
  • Integrate sendbird with Angular6
  • Single line command to iterate & find over multiple items and update Mongoose?
  • Need some clarification on using node_modules
  • Is there a way to Cancel Apple Subscriptions from a nodejs server?
  • Node.js Semicolon Use
  • webContents.send and ipcRenderer.on Not Working
  • Why can I not connect to the mongoose server
  • npm global install of ganache-cli fails (because of sudo/permissions)
  • Catching plugin issues in hapi 17
  • Proper way to use Stripe's JS SDK on behalf of Connect accounts
  • Cannot connect to mongodb container
  • Changing nodemailer "from" filed to a variable
  • QuerySnapshot.docChanges has been changed from a property into a method
  • NPM: Cannot find module 'jest-serializer' (when globally installed only)
  • Property 'forEach' does not exist on type '(options?: SnapshotListenOptions)
  • Invalid redirect error in Chrome after migrating to CloudFlare
  • Mongoose model architecture and Node require order
  • How to get Conference Sid at the time of dialing twilio call
  • How to implement an API in SCP with Node.js and display on Browser
  • Updating node.js causes 'write EPIPE' with ng e2e
  • Socket.io in sub-file and outside on('connect)
  • Telegram bot : Button text is not readable
  • Facebook graph API - i recieve the error "Missing authorization code"?
  • What Is The Purpose of Node.Exe's Program Window
  • How to do aggregation in nodeJS and mongoDB
  • Using a Node packages with Electron return me an `ERR_CONNECTION_REFUSED`
  • NodeJS how to execute a function only on working hours?
  • HTML5 canvas with Node.js, clustering and Socket.IO
  • Puppeteer fails to navigate to url (ERR_EMPTY_RESPONSE)
  • making a GET request inside a NodeJs app using express
  • Getting all data in collection in Firestore on NodeJS
  • Loopback 4 Debugger nodemon Solution
  • How to able/start a pm2 module without restarting my pc or reinstall this module?
  • How to fix this issue with undefined variable Node js?
  • Puppeteer ignores proxy setting
  • Trigger client-side reload in next.js
  • Lambda: module initialization error: Error
  • Why my node js express app not keep alive?
  • Facing Issue in Creating/Setting up React Native Project on System
  • socket.io-client-cpp does not connect to node app on Heroku via https
  • Unable to thrown an error in Express Node
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org