Created
May 31, 2012 16:27
-
-
Save rainforest-of-code/2844582 to your computer and use it in GitHub Desktop.
Follow along code for a Jasmine introduction presentation
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
function Ball() { | |
var self = this; | |
var full = false; | |
self.inflate = function() { | |
full = true; | |
}; | |
self.isFull = function() { | |
return full; | |
}; | |
return self; | |
} |
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("Ball", function () { | |
var ball; | |
beforeEach(function() { | |
ball = new Ball(); | |
}); | |
it("should start deflated", function() { | |
expect(ball.isFull()).toEqual(false); | |
}); | |
describe("#inflate", function () { | |
beforeEach(function() { | |
ball.inflate(); | |
}); | |
it("should fill a ball", function() { | |
expect(ball.isFull()).toEqual(true); | |
}); | |
it("should not affect an already inflated ball", function() { | |
ball.inflate(); | |
expect(ball.isFull()).toEqual(true); | |
}); | |
}); | |
}); |
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
function Game() { | |
var self = this; | |
self.prepare = function(ball) { | |
if (ball.isFull()) { | |
return; | |
} | |
ball.inflate(); | |
}; | |
return self; | |
} |
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("Game", function () { | |
var game, ball; | |
beforeEach(function() { | |
ball = new Ball(); | |
spyOn(ball, 'inflate').andCallThrough(); | |
game = new Game(); | |
}); | |
describe("with a full ball", function() { | |
beforeEach(function() { | |
ball.inflate(); | |
ball.inflate.reset(); | |
game.prepare(ball); | |
}); | |
it("should not inflate before play", function() { | |
expect(ball.inflate).not.toHaveBeenCalled(); | |
}); | |
}); | |
describe("with a not-full ball", function() { | |
beforeEach(function() { | |
game.prepare(ball); | |
}); | |
it("should inflate before play", function() { | |
expect(ball.inflate).toHaveBeenCalled(); | |
}); | |
}); | |
}); |
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("When using the Jasmine Ajax Mock", function() { | |
var onSuccess, onComplete, onFailure, request; | |
beforeEach(function() { | |
onSuccess = jasmine.createSpy('onSuccess'); | |
onComplete = jasmine.createSpy('onComplete'); | |
onFailure = jasmine.createSpy('onFailure'); | |
jasmine.Ajax.useMock(); | |
jQuery.ajax({ | |
url: "example.com/someApi", | |
success: onSuccess, | |
complete: onComplete, | |
error: onFailure | |
}); | |
request = mostRecentAjaxRequest(); | |
}); | |
it("prevents the call from leaving your system", function() { | |
expect(request.url).toEqual("http://example.com/someApi"); | |
}); | |
describe("with a successful response", function() { | |
beforeEach(function() { | |
var successResponse = { | |
status: 200, | |
responseText: "w00t!" | |
}; | |
request.response(successResponse); | |
}); | |
it("should call the success callback", function() { | |
expect(onSuccess).toHaveBeenCalledWith("w00t!"); | |
}); | |
it("should call the complete callback", function() { | |
expect(onComplete).toHaveBeenCalled(); | |
}); | |
}); | |
describe("with a successful response", function() { | |
beforeEach(function() { | |
var failureResponse = { | |
status: 500, | |
responseText: "Doh!" | |
}; | |
request.response(failureResponse); | |
}); | |
it("should call the failure callback", function() { | |
expect(onFailure).toHaveBeenCalledWith("Doh!"); | |
}); | |
it("should call the complete callback", function() { | |
expect(onComplete).toHaveBeenCalled(); | |
}); | |
}) | |
}); |
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("Manually ticking the Jasmine Mock Clock", function() { | |
var timerCallback; | |
beforeEach(function() { | |
timerCallback = jasmine.createSpy('timerCallback'); | |
jasmine.Clock.useMock(); | |
}); | |
it("causes a timeout to be called synchronously", function() { | |
setTimeout(function() { | |
timerCallback(); | |
}, 100); | |
expect(timerCallback).not.toHaveBeenCalled(); | |
jasmine.Clock.tick(101); | |
expect(timerCallback).toHaveBeenCalled(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment