Expects

We test the functionality of our code by writing expects that are no more than simple assumptions that we have for our code if everything was implemented correctly. In the file we are working on, apply the following changes:

...

describe("Calculator", function() {

it("should return the addition ", function() {
expect(Calculator.add(1, 2)).toEqual(3);
});

it("should return the substraction ", function() {
expect(Calculator.substract(1, 2)).toEqual(-1);
});

it("should return the multiplication ", function() {
expect(Calculator.multiply(1, 2)).toEqual(2);
})

it("should return the division ", function() {
expect(Calculator.divide(1, 2)).toEqual(0.5);
})

});

We have created the expectations that we have for our code. For example, for the additional test case, we expect that the result of add(1, 2) returns 3. We specify the match operation using helper functions, such as the toEqual function, that as its name says, won't raise any exception in case the result of calling the add function is the same as the expected value.

Now that we have our test script fully implemented, let's execute it and see what we get in the console. Run the following command:

$ jasmine

Started
....


4 specs, 0 failures
Finished in 0.006 seconds

Now the output displayed is that 4 specs were found and executed without failures. It is time to see what will happen if we force a test case to fail. Replace the expect statement in the addition test case for the following expects:

...

it("should return the addition ", function() {
expect(Calculator.add(1, 2)).toEqual(30);
});

...

Once the changes are applied, run the following command to make our test fail:

$ jasmine 

Started
F...

Failures:
1) Calculator should return the addition
Message:
Expected 3 to equal 30.
Stack:
Error: Expected 3 to equal 30.
...

4 specs, 1 failure
Finished in 0.011 seconds

Let's analyze the output. The first thing to note is the following F... string, this means that the first test has failed and the other 3 (dots) are correct. You can also read the Expected 3 to equal 30 message giving the reason why our test fails, and finally the test resume that shows that 4 specs where found but just 1 failure occurred.

The output for this test displays the failing test at the beginning of the F... string. This might be at the beginning or in another order. That is because the random property in the jasmine.json file is configured to be true, which means random executions. If you want to execute your tests sequentially, change the random property to false.

You have seen how easy it is to use the Jasmine framework to test our code. Of course, you have to learn more about it; we would really like to teach you everything about Jasmine, but that is beyond the scope of this book. We recommend you visit the official site at https://jasmine.github.io/.

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

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