Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

examples of testing with jasmine

View gist:846908
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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;
}
View gist:846908
1 2 3 4 5 6 7 8
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);
});
});
View gist:846908
1 2 3 4 5 6 7 8 9 10
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();
});
});
View gist:846908
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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);
});
*/
});

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

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
Something went wrong with that request. Please try again.