In the first
Jasmine example, we used the toEqual
and toThrow
Jasmine matchers. In this section, the other different built-in matchers provided by Jasmine will be illustrated and will explain how to write a custom Jasmine matcher to have more powerful and descriptive testing code.
The toBe
matcher is passed if the actual value is of the same type and value as that of the expected value. It uses ===
to perform this comparison. The following code snippet shows an example of the toBe
matcher:
describe("the toBe Matcher", function() { it("should compare both types and values", function() { var actual = "123"; var expected = "123"; expect(actual).toBe(expected); }); });
You might question the difference between the toBe
and toEqual
matchers. The answer to this question would be that the toEqual
matcher provides a powerful mechanism for handling equality; it can handle array comparisons, for example, as shown in the following...