Skip to content

Instantly share code, notes, and snippets.

@Oleg-Sulzhenko
Last active February 27, 2019 14:11
Show Gist options
  • Save Oleg-Sulzhenko/0304fb0f5b99ed5c8b24299b05c3b14a to your computer and use it in GitHub Desktop.
Save Oleg-Sulzhenko/0304fb0f5b99ed5c8b24299b05c3b14a to your computer and use it in GitHub Desktop.
//This is test suite
describe("Test Suite", function() {
it("test spec", function() {
expect( expression ).toEqual(true);
});
});
-------------------------------------------------------------------------
Matchers
toBe() passed if the actual value is of the same type and value as that of the expected value. It compares with === operator
toEqual() works for simple literals and variables;
should work for objects too
toMatch() to check whether a value matches a string or a regular expression
toBeDefined() to ensure that a property or a value is defined
toBeUndefined() to ensure that a property or a value is undefined
toBeNull() to ensure that a property or a value is null.
toBeTruthy() to ensure that a property or a value is true
ToBeFalsy() to ensure that a property or a value is falseToBe
toContain() to check 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 if a function throws an exception
toThrowError() for testing a specific thrown exception
-------------------------------------------------------------------------
Spy
var calc;
beforeEach(function() {
calc = new MathUtils();
spyOn(calc, 'sum');
});
it("should be able to calculate sum of 3 and 5", function() {
//call any method
calc.sum(3,5);
//verify it got executed
expect(calc.sum).toHaveBeenCalled();
expect(calc.sum).toHaveBeenCalledWith(3,5);
});
====================================
tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
tape.play();
//Use it for testing
expect(tape.play.calls.any()).toEqual(true);
.calls.any() returns false if the spy has not been called at all, and then true once at least one call happens.
.calls.count() returns the number of times the spy was called
.calls.argsFor(index) returns the arguments passed to call number index
.calls.allArgs() returns the arguments to all calls
.calls.all() returns the context (the this) and arguments passed all calls
.calls.mostRecent() returns the context (the this) and arguments for the most recent call
.calls.first() returns the context (the this) and arguments for the first call
.calls.reset() clears all tracking for a spy
--------------------------------------------------------------------------
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BannerComponent } from './banner.component';
describe('BannerComponent (minimal)', () => {
let component: BannerComponent;
let fixture: ComponentFixture<BannerComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ BannerComponent ]
});
fixture = TestBed.createComponent(BannerComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeDefined();
});
it('should contain "banner works!"', () => {
const bannerElement: HTMLElement = fixture.nativeElement;
const p = bannerElement.querySelector('p');
expect(bannerElement.textContent).toContain('banner works!');
expect(p.textContent).toEqual('banner P works!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment