Test cases

To write the test for our applications, we need test cases that should basically consider all the possible scenarios that your app can afford. For our Calculator example, we will create test cases for the four main arithmetic operations—addition, multiplication, subtraction, and division. Test cases are created using the it function, as follows:

describe("Calculator", function() {

it("should return the addition ", function() {
...
});

it("should return the substraction ", function() {
...
});

it("should return the multiplication ", function() {
...
})

it("should return the division ", function() {
...
})

});

Before we continue on our trip, we should have something to test. So let's create a Calculator object to test out. In the same calculator.spec.js file, add the following code at the very beginning, before the suite declaration, as follows:

var Calculator = {
add: function(n1, n2) {
return n1 + n2;
},
substract: function(n1, n2) {
return n1 - n2;
},
multiply: function(n1, n2) {
return n1 * n2;
},
divide: function(n1, n2) {
return n1 / n2;
}
}


describe("Calculator", function() {
...

Now that we have something to test, let's learn about how we can write assertions in our test file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset