Skip to content

Instantly share code, notes, and snippets.

@joneath
Created January 26, 2012 00:48
Show Gist options
  • Save joneath/1680082 to your computer and use it in GitHub Desktop.
Save joneath/1680082 to your computer and use it in GitHub Desktop.
Basic Backbone View Jasmine Test
describe("NotesView", function() {
beforeEach(function() {
var notes = new NotesCollection();
spyOn(notes, "fetch");
var view = new NotesView({collection: notes});
});
describe("#initialize", function() {
it("should fetch the notes", function() {
expect(notes.fetch).toHaveBeenCalled();
});
describe("on notes fetch success", function() {
beforeEach(function() {
var request = mostRecentAjaxRequest();
request.response({
status: 200,
responseText: JSON.stringify([
{body: "Blog post #1", id: "1"}
])
});
});
it("should render the view", function() {
expect($(view.el).find(".post")).toHaveText("Blog post #1");
});
});
});
});
@dguzzo
Copy link

dguzzo commented Mar 21, 2013

i'm pretty sure that you aren't scoping your Backbone objects properly; do these specs actually pass?

you'll probably want

var notes, view;

above & outside the beforeEach() block.

then within the beforeEach(), you'd have the same assignment minus the "var" in front.

make sense? or am I off base/using an outdated Jasmine version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment