cyrf
cyrf

Reputation: 6023

mock StripeCheckout in Jasmine test

I am writing Jasmine tests for my Javascript application. A major time-sink has been testing my code which depends on StripeCheckout. Stripe does not want you to use it offline. I realized I should mock the service, but that has not been easy in Jasmine.

How can I mock "custom" (instead of "simple") usage of StripeCheckout?

I tried to use spies, like so,

var StripeCheckout = jasmine.createSpyObj('StripeCheckout', ['configure']);

But I think the created object needs to attach to the global object (window).

Upvotes: 2

Views: 532

Answers (1)

tehbeardedone
tehbeardedone

Reputation: 2858

I'm not sure if you have figured this out yet but an easy way to do this would be to just create a simple mocked implementation for StripeCheckout. Something like this should work.

beforeEach(function() {
    module(function($provide) {
        $provide.factory('StripeCheckout', function() {
            //your mocked custom implementation goes here

            configure: function() {
                //do something   
            }
        }
    }

    inject(function($injector) {
        StripeCheckoutMock = $injector.get('StripeCheckout');
    }
)

This way you are not making a request in order to run your tests. The test will just use the mocked service that you have set up.

Upvotes: 1

Related Questions