Skip to content

Instantly share code, notes, and snippets.

@dougalcorn
Created September 12, 2011 19:52
Show Gist options
  • Select an option

  • Save dougalcorn/1212205 to your computer and use it in GitHub Desktop.

Select an option

Save dougalcorn/1212205 to your computer and use it in GitHub Desktop.
testing backbone models using the right backend endpoints
/* This is in SpecHelper.js */
$(function() {
beforeEach(function() {
this.addMatchers({
toRequest: function(expectedRequest) {
var actualRequest, name, value;
actualRequest = this.actual;
for (name in expectedRequest) {
value = expectedRequest[name];
this.actual = actualRequest[name];
this.message = function() {
return "Expected " + name + " to be '" + value + "' but got '" + this.actual + "'";
};
if (this.actual !== value) {
return false;
}
}
return true;
}
});
});
});
(function() {
describe("Asset", function() {
return describe("syncing to backend", function() {
beforeEach(function() {
jasmine.Ajax.useMock();
this.response = {
status: 200,
responseText: "{}"
};
return this.asset = new Asset;
});
describe("fetching with the id", function() {
beforeEach(function() {
this.asset.set({
guid: "1234"
});
this.asset.fetch(); // this is Backbone's method that should do an ajax request for the model
this.request = mostRecentAjaxRequest(); // grab the mocked out ajax request
return this.request.response(this.response);
});
return it("request matches expectations", function() {
// verify the ajax request is what we expected it to be
return expect(this.request).toRequest({
method: "GET",
url: "/entities/assets/1234",
params: null
});
});
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment