Testing Angular JS
- generate a generator-karma + generator-angular app
(additional npm modules (grunt-karma) may be needed if not installed by generator.)
- ‘grunt test' should run the tests
describe an entity describe('...', function() {});
specify a testing case it('...', function() {});
expect some behaviour expect(entity) ...
=== comparison .toBe();
negate an expectation .not.
compare .toEqual();
match with regex .toMatch("xyz"); OR .toMatch(/\w+Dr\./);
check if undefined .toBeUndefined();
check if null .toBeNull();
check if truthy .toBeTruthy();
check if falsy .toBeFalsy();
check for array elements / substrings .toContain();
check if smaller .toBeLessThan();
check if greater .toBeGreaterThan();
check for precision within n decimal places .toBeCloseTo(X, n);
chek if function throws error .toThrow();
check if function throws a specific error .toThrowError();
setup spec beforeEach(function() {});
tear–down spec afterEach(function() {});
share data between specs beforeEach(function() { this.data = ...; });
disable a description xdescribe('...', function() {});
pend a spec xit('...', function() {}); or it('...'); OR pending();
spy on a method spyOn(someObject, 'methodName');
check if method has been called .toHaveBeenCalled(); OR .calls.any();
check if method has been called a specific number of times .toHaveBeenCalledTimes(); OR .calls.count();
check if method has been called with specific parameter(s) .toHaveBeenCalledWith();
get arguments passed with Nth call .calls.argsFor(N);
get arguments passed with all counts .calls.allArgs();
get context and all passed arguments .calls.all();
get context and arguments for last call .calls.mostRecent();
get context and arguments for first call .calls.first();
let implementation run in addition to spying .and.callThrough();
let spy fake a return value .and.returnValue();
let spy fake return values (& undefined when run out) .and.returnValues();
let spy fake call .and.callFake(function() {});
let spy throw error .and.throwError();
let spy go back to state before callThrough .and.stub();
clear tracking for a spy .calls.reset();
check for a value's constructor .toEqual(jasmine.any(constructor));
check if defined and non–null .toEqual(jasmine.anything());
check if object contains a property–value pair .toEqual(jasmine.objectContaining({key: value}));
check if array contains a property–value pair .toEqual(jasmine.arrayContaining([value1 ,value2]));
check if string matches regex .toEqual(jasmine.stringMatching(regex));
define custom matcher function .toEqual({asymmetricMatch: function(actual) { return actual. ... === '...'}});
install/uninstall timer jasmine.clock().install()/uninstall();
move timer forward jasmine.clock().tick(...);
mock the current date jasmine.clock().mockDate(new Date(...., .., ..));