www.briebug.com
CEO, BrieBug
Developer for 20 years
Focused on JavaScript based solutions
About Me
www.briebug.com
www.briebug.com
www.briebug.com
Definition: Unit Testing is a level of software testing where individual function/method of a software are tested. The purpose is to validate that each function of the software performs as designed. This is normally performed by software developers.
www.briebug.com
Jasmine vs Mocha?
Karma.JS
Karma.conf.js
Code Coverage
www.briebug.com
www.briebug.com
/* tslint:disable:no-unused-variable */
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {DebugElement} from '@angular/core';
import {MyComponentComponent} from './my-component.component';
describe('MyComponentComponent', () => {
let component: MyComponentComponent;
let fixture: ComponentFixture<MyComponentComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponentComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
www.briebug.com
fixture.detectChanges
ngOnInit
Router Calls
www.briebug.com
www.briebug.com
Service/Http Calls
Mobile Native Library Calls
Router functions
Long running processes
Anything you want to isolate from outside function calls.
You control the return values...
Pros:
Cons:
www.briebug.com
Pros:
Cons:
www.briebug.com
RouterTestingModule vs spyOn
www.briebug.com
test.module.ts
www.briebug.com
www.briebug.com
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule, ReactiveFormsModule, NgForm} from '@angular/forms';
import {TranslateModule, TranslateService} from 'ng2-translate/ng2-translate';
import {ToastModule, ToastsManager} from 'ng2-toastr/ng2-toastr';
import {AuthService, SessionService} from '../app/services';
import {ToastsManagerMock, AuthServiceMock, SessionServiceMock} from './index';
@NgModule({
imports: [
CommonModule, ReactiveFormsModule, FormsModule, TranslateModule.forRoot(), ToastModule
],
declarations: [],
providers: [
TranslateService,
NgForm,
{provide: AuthService, useClass: AuthServiceMock},
{provide: SessionService, useClass: SessionServiceMock},
{provide: ToastsManager, useClass: ToastsManagerMock}
],
exports: [
CommonModule, ReactiveFormsModule, FormsModule, TranslateModule, ToastModule
]
})
export class TestModule {}
// arrange // act // assert
www.briebug.com
www.briebug.com
www.briebug.com
"HTML Testing"
www.briebug.com
www.briebug.com
www.briebug.com
www.briebug.com
import {DebugElement} from '@angular/core';
import {dispatchEvent} from '@angular/platform-browser/testing/browser_util';
import {By} from '@angular/platform-browser';
New Imports
www.briebug.com
let usernameEl: any,
passwordEl: any,
usernameErrorEl: any,
passwordErrorEl: any,
loginButtonEl: DebugElement,
loginForm: FormGroup,
usernameFormControl: AbstractControl,
passwordFormControl: AbstractControl...
beforeEach(() => {
// call ngOnInit
fixture.detectChanges();
// get the html elements
usernameEl = fixture.debugElement.query(By.css('#username')).nativeElement;
passwordEl = fixture.debugElement.query(By.css('#password')).nativeElement;
loginButtonEl = fixture.debugElement.query(By.css('.btn-primary'));
// Get the form and form controls
loginForm = component.loginForm;
usernameFormControl = loginForm.controls['username'];
passwordFormControl = loginForm.controls['password'];
});
Select your controls
*ng-if - my control isn't there during beforeEach
www.briebug.com
browser utility that updates the form group with the new value.
www.briebug.com
www.briebug.com
myDropdownEl.value = 2;
dispatchEvent(statusEl, 'change');
dueDateEl.value = '01/01/2016';
dispatchEvent(dueDateEl, 'change');
Text
When in doubt use console log to inspect.
www.briebug.com
spyOn(router, 'navigate');
www.briebug.com
www.briebug.com
it('form should show validators when invalid', async(() => {
// arrange
fixture.whenStable().then(() => {
// act
myButton.triggerEventHandler('click', null);
fixture.detectChanges();
// assert
expect(someValue).toBe('abc');
});
}));
fixture.whenStable
www.briebug.com
myButton.triggerEventHandler('click', null);
fixture.detectChanges();
triggerEventHandler
www.briebug.com