Skip to content

Instantly share code, notes, and snippets.

@nitely
Last active August 29, 2015 14:03
Show Gist options
  • Save nitely/f1ad2614abf80b60de77 to your computer and use it in GitHub Desktop.
Save nitely/f1ad2614abf80b60de77 to your computer and use it in GitHub Desktop.
jasmine 2.0 test ajax/post/getJSON/etc in jQuery
describe "post tests", ->
post = null
beforeEach ->
#jasmine-jquery
#fixtures = do jasmine.getFixtures
#fixtures.fixturesPath = 'base/test/fixtures/'
#loadFixtures 'bookmark.html'
post = spyOn $, 'post'
d = $.Deferred()
post.and.callFake (req) =>
data = {foo: "bar", }
d.resolve(data) # success
#d.reject(data) # failure
return d.promise()
it "makes the post", ->
expect($.post.calls.any()).toEqual false
$.post "/foo/", {foobar: "foo", }
expect($.post.calls.any()).toEqual true
expect($.post.calls.argsFor(0)).toEqual ['/foo/', {foobar: "foo", }]
it "let you spy on promises", ->
always = spyOn post(), 'always'
post.calls.reset()
expect($.post.calls.any()).toEqual false
expect(always.calls.any()).toEqual false
p = $.post "/foo/", {foobar: "foo", }
p.always ->
# do something
expect($.post.calls.any()).toEqual true
expect(always.calls.any()).toEqual true
// Generated by CoffeeScript 1.7.1
(function() {
describe("post tests", function() {
var post;
post = null;
beforeEach(function() {
var d;
post = spyOn($, 'post');
d = $.Deferred();
return post.and.callFake((function(_this) {
return function(req) {
var data;
data = {
foo: "bar"
};
d.resolve(data);
return d.promise();
};
})(this));
});
it("makes the post", function() {
expect($.post.calls.any()).toEqual(false);
$.post("/foo/", {
foobar: "foo"
});
expect($.post.calls.any()).toEqual(true);
return expect($.post.calls.argsFor(0)).toEqual([
'/foo/', {
foobar: "foo"
}
]);
});
return it("let you spy on promises", function() {
var always, p;
always = spyOn(post(), 'always');
post.calls.reset();
expect($.post.calls.any()).toEqual(false);
expect(always.calls.any()).toEqual(false);
p = $.post("/foo/", {
foobar: "foo"
});
p.always(function() {});
expect($.post.calls.any()).toEqual(true);
return expect(always.calls.any()).toEqual(true);
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment