Create a gist now

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@victormartins

victormartins Jan 22, 2012

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

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

This comment has been minimized.

Show comment
Hide comment
@joelmsanto

joelmsanto Aug 20, 2012

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

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

@rebekah

This comment has been minimized.

Show comment
Hide comment
@rebekah

rebekah Sep 4, 2012

Thanks Rebecca!

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