Skip to content

Instantly share code, notes, and snippets.

@cjohansen
Created October 5, 2010 07:14
Show Gist options
  • Select an option

  • Save cjohansen/611155 to your computer and use it in GitHub Desktop.

Select an option

Save cjohansen/611155 to your computer and use it in GitHub Desktop.
describe("My webservices AJAX module", function () {
var server;
beforeEach(function () {
server = sinon.useFakeServer();
server.respondWith("GET", "Products.asmx",
[200, { "Content-Type": "application/json" }, "{ 'some': 'data' }"]);
});
afterEach(function () {
server.restore();
});
it("should make an AJAX request", function () {
var callback = sinon.spy();
webServices.getProducts(callback);
server.respond();
expect(callback.callCount).toEqual(1);
// Optionally:
// expect(server.requests.length).toEqual(1);
});
it("should make an AJAX request to the URL 'Products.asmx'", function () {
var spy = sinon.spy();
webServices.getProducts(callback);
server.respond();
expect(callback.calledWith({ some: "data" })).toEqual(true);
// Optionally
// expect(server.requests[0].url).toEqual("Products.asmx");
});
it("should make an AJAX request of dataType 'json'", function () {
webServices.getProducts();
server.respond();
expect(server.requests[0].getRequestHeader("Content-Type")).toEqual("application/json");
});
it("should call through to the success callback function", function () {
var callback = sinon.spy();
webServices.getProducts(callback);
server.respond();
expect(callback.callCount).toEqual(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment