Created
February 28, 2011 03:47
-
-
Save rmurphey/846908 to your computer and use it in GitHub Desktop.
examples of testing with jasmine
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 multiply(a, b) { | |
return a * b; | |
} | |
function divide(a, b) { | |
if (b === 0) { | |
throw "Don't try to divide by zero!"; | |
} | |
return Math.round(a / b); | |
} | |
function createList(items) { | |
var list = $('<ul/>'); | |
$.each(items, function(i, item) { | |
if (typeof item !== 'string') { return; } | |
list.append( | |
$('<li>' + item + '</li>') | |
.click(function() { | |
$(this).addClass('clicked'); | |
}) | |
); | |
}); | |
return list; | |
} |
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("multiply", function() { | |
it("should return the product of two numbers", function() { | |
expect(multiply(2,3)).toEqual(6); | |
expect(multiply(-1,1)).toEqual(-1); | |
expect(multiply(1,0)).toEqual(0); | |
expect(multiply(-1,-1)).toEqual(1); | |
}); | |
}); |
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("divide", function() { | |
it("should return the value of a / b", function() { | |
expect(divide(3,3)).toEqual(1); | |
expect(divide(6,2)).toEqual(3); | |
}); | |
it("should throw an error if you try to divide by 0", function() { | |
expect(function() { divide(1,0); }).toThrow(); | |
}); | |
}); |
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("createList", function() { | |
var items = [ 'apple', 'orange', 'pear' ]; | |
it("should return a jquery object containing an unordered list", function() { | |
var list = createList(items); | |
expect(list).toBeDefined(); | |
expect(list.jquery).toBeDefined(); | |
expect(list.length).toBe(1); | |
expect(list[0].nodeName.toLowerCase()).toBe('ul'); | |
}); | |
it("should contain the correct number of list items", function() { | |
var list = createList(items); | |
expect(list.children().length).toBe(3); | |
expect(list.children('li').length).toBe(list.children().length); | |
}); | |
it("should properly populate the list items", function() { | |
var list = createList(items); | |
expect(list.children().eq(1).html()).toEqual('orange'); | |
}); | |
it("should only use an item if it is a string", function() { | |
var list = createList([ 'apple', 1, false, {}, [], 'pear' ]); | |
expect(list.children().length).toBe(2); | |
}); | |
it("should add a click handler to its list items that adds a class of 'clicked'", function() { | |
var list = createList(items), | |
li = list.find('li').click(); | |
expect(li.hasClass('clicked')).toBeTruthy(); | |
}); | |
/* | |
it("should only use items that are at least three characters long", function() { | |
var list = createList([ 'a', 'long enough' ]); | |
expect(list.children().length).toBe(1); | |
}); | |
*/ | |
}); |
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
Hi Rebecca,
now I see your tutorial, have a better idea about unit testing!
Thanks Rebecca!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you Rebecca, I've learned a lot from your example!
However I changed this, to be cleaner:
expect(list[0].nodeName.toLowerCase()).toBe('ul');
expect(list.attr("nodeName")).toBe("UL");