Spec

A spec is a test case inside the test suite. A test suite comprises one or more test specs. A test spec comprises a describe function and one or more it functions. An it function is comprised of two parameters, one of which is a string and the other of which is a function that implements the test case. Treat this like an Apex test method.

A spec also comprises of expect statements that are used for asserting the behavior. Treat asserts are equal to system.assert in Apex test methods. The expect statements compare the actual object against the matcher object. There are out-of-box matchers available. You can refer to the API guide to learn more about them at https://jasmine.github.io/api/edge/matchers.html.

The following table describes the out-of-box matchers that are available:

Matcher Definition
toBe()

Passed if the actual value is of the same type and value as that of the expected value. It compares with the === operator.

toEqual()

Works for simple literals and variables; should work for objects too.

toMatch()

Checks whether a value matches a string or a regular expression.

toBeDefined()

Ensures that a property or a value is defined.

toBeUndefined()

Ensures that a property or a value is undefined.

toBeNull()

Ensures that a property or a value is null.

toBeTruthy()

Ensures that a property or a value is true.

toBeFalsy()

Ensures that a property or a value is false.

toContain()

Checks whether a string or array contains a substring or an item.

toBeLessThan()

For mathematical comparisons of less than.

toBeGreaterThan()

For mathematical comparisons of greater than.

toBeCloseTo()

For precision math comparison.

toThrow()

For testing whether a function throws an exception.

toThrowError()

For testing a specific thrown exception.

 

For example, if we want to test a string util function and perform tests on its behavior for camelcasing, concatenation, and so on, the code syntax would look as follows:

describe("StringUtils", function() {
var stringUtil;

//This will be called before running each spec
beforeEach(function() {
stringUtil = new StringUtils();
});

describe("when string operations are performed", function(){

//Spec for Concatenation operation
it("should be able to concatenate hello and world", function() {
expect(stringUtil.concatenate(Hello,World)).toEqual(HelloWorld);
});

//Spec for camelcase operation
it("should be able to camelcase", function() {
expect(stringUtil.camelcase('hello-world')).toEqual('HelloWorld');
});

});
});
..................Content has been hidden....................

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