Created
October 23, 2013 18:21
-
-
Save seanhagen/7123848 to your computer and use it in GitHub Desktop.
Example unit tests for Backbone.js model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe( "Video model", function(){ | |
var MOCK_GET_DATA = { | |
channelName: "tgndeveloperedu", | |
commentCount: 0, | |
defaultOrder: 0, | |
dislikeCount: 0, | |
facebookCount: "0", | |
googleCount: "0", | |
likeCount: 0, | |
optimized: false, | |
originalSrc: null, | |
overlaySrc: null, | |
pinterestCount: "0", | |
published: "2013-10-08 13:58:28", | |
revenue: 0, | |
schedulerPublishDate: null, | |
schedulerSelectedTz: null, | |
schedulerStatus: null, | |
selectedThumbnailSrc: null, | |
smallOriginalSrc: null, | |
status: "private", | |
subscribersGained: 0, | |
thumbnailOptimized: false, | |
thumbnailPublishDate: false, | |
thumbnailsAvailable: false, | |
title: "sample_iPod 2.m4v", | |
twitterCount: "0", | |
updatedThumbnailSrc: null, | |
updatedThumbnailUploaded: null, | |
uploadedThumbnailSrc: null, | |
videoId: "7O_vD_3Qmss", | |
videoUploaderStatus: "8", | |
viewCount: 0, | |
watchTime: 0 | |
}; | |
var MOCK_POST_DATA = { | |
success: true | |
}; | |
describe( "when it fetches", function(){ | |
var model; | |
var spy; | |
beforeEach( function(){ | |
//spy = sinon.spy( jQuery, "ajax" ); | |
model = new VideoModel({videoId: '7O_vD_3Qmss'}); | |
this.server = sinon.fakeServer.create(); | |
this.server.respondWith( | |
"GET", | |
"/api/video/7O_vD_3Qmss", | |
[ | |
200, | |
{ "Content-Type": "application/json" }, | |
JSON.stringify( MOCK_GET_DATA ) | |
] | |
); | |
model.fetch(); | |
this.server.respond(); | |
}); | |
afterEach( function(){ | |
//jQuery.ajax.restore(); | |
model = null; | |
spy = null; | |
this.server.restore(); | |
}); | |
it('should be able to parse mocked service response', function(){ | |
expect(_.isEmpty(model.attributes)).to.equal(false); | |
expect( model.get('channelName') ).to.equal( MOCK_GET_DATA.channelName ); | |
expect( model.get('published') ).to.equal( MOCK_GET_DATA.published ); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment