Search code examples
unit-testingjasminesinon

Mock Third party library (Razorpay) in Angular Unit tests?


I am trying to test a function in one of my component which consists following two lines:

this.rzp1 = new Razorpay(orderDetails);
this.rzp1.open();

I am trying to understand how to mock Razorpay in my test cases for this function.

This is how I am declaring Razorpay in my component:

export declare var Razorpay: any;

I have already tried various methods like:

var stub = sinon.createStubInstance(MyConstructor)

Any leads will be helpful.


Solution

  • Why not use jasmine's built in spy facility instead of relying on another library (sinon)?

    In your before each block, you can do something like this:

    beforeEach(() => {
      jasmine.spyOnAllFunctions(Razorypay.prototype);
      Razorypay.prototype.open.and.returnValue('foo');
    });
    

    You can find more information about spyOnAllFuntions in the documentation.