Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine tests that require outside libraries

I was wondering what is best practice or at least a practice for using Jasmine to test javascript that requires remote libraries called on page load, but not in the app.

More specifically, I'm creating a backbone view for processing payments using stripe. Stripe recommends that you load their javascript in your layout from their servers.

But my tests don't have my layout, so when I try to do this

it("calls stripe token creation", function() {
  stripeSpy = spyOn(Stripe, "createToken");
  form.submit();
  expect(stripeSpy).toHaveBeenCalled();
});

It gives the error.

Stripe is not defined

I'd rather not depend on remote libraries for my test, nor do I really want to go against stripe preferred method of relying on their source code. What would be the best way to approach this?

like image 260
Eric C Avatar asked Sep 18 '13 23:09

Eric C


2 Answers

To mock out the Stripe dependency you have to create a new Stripe object with the function you want to call:

var Stripe = {createToken: sinon.spy()} 
form.submit();
expect(Stripe.createToken).toHaveBeenCalled();
like image 188
Andreas Köberle Avatar answered Sep 29 '22 07:09

Andreas Köberle


You can use Sinon.JS to mock\stub this out.

Check out http://sinonjs.org/

Example usage for jQuery's AJAX stub

it("makes a GET request for todo items", function () {
    sinon.stub(jQuery, "ajax");
    getTodos(42, sinon.spy());

    assert(jQuery.ajax.calledWithMatch({ url: "/todo/42/items" }));
});

For yours I'd imagine you'd do something like sinon.stub(Stripe, "createToken")

Hope this helps.

like image 34
Kyle Muir Avatar answered Sep 29 '22 06:09

Kyle Muir