Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created February 28, 2011 03:47
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rmurphey/846908 to your computer and use it in GitHub Desktop.
Save rmurphey/846908 to your computer and use it in GitHub Desktop.
examples of testing with jasmine
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;
}
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);
});
});
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();
});
});
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);
});
*/
});
@victormartins
Copy link

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");

@joelmsanto
Copy link

Hi Rebecca,
now I see your tutorial, have a better idea about unit testing!

@rebekah
Copy link

rebekah commented Sep 4, 2012

Thanks Rebecca!

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