final unittest

unit-test
Florian Hartwich 2017-04-17 08:26:39 +02:00
parent 3d98366a1c
commit 89f5b2d1ab
9 changed files with 169 additions and 81 deletions

View File

@ -8,8 +8,9 @@ import {BlogComponent} from "../blog.component";
styleUrls: ['../blog.component.css'] styleUrls: ['../blog.component.css']
}) })
export class BlogEntryComponent { export class BlogEntryComponent {
@Input() blogComponent: BlogComponent;
@Input() entry: BlogEntry; @Input() entry: BlogEntry;
@Input() blogComponent: BlogComponent;
deleteBlogEntry(id: number) { deleteBlogEntry(id: number) {
this.blogComponent.deleteBlogEntry(id); this.blogComponent.deleteBlogEntry(id);

View File

@ -1,5 +1,7 @@
<h1>Angular-Blog</h1> <h1>Angular-Blog</h1>
<div id="entry-list">
<blog-entry *ngFor="let entry of entries" [entry]="entry" [blogComponent]="this"></blog-entry> <blog-entry *ngFor="let entry of entries" [entry]="entry" [blogComponent]="this"></blog-entry>
</div>
<h2>Neuen Blog-Eintrag erstellen</h2> <h2>Neuen Blog-Eintrag erstellen</h2>
<div class="form"> <div class="form">

View File

@ -1,18 +1,26 @@
import {BlogComponent} from './blog.component' import {BlogComponent} from './blog.component'
import {TestBed} from "@angular/core/testing"; import {async, TestBed} from "@angular/core/testing";
import {RouterTestingModule} from "@angular/router/testing"; import {RouterTestingModule} from "@angular/router/testing";
import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core"; import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core";
import {By} from "@angular/platform-browser"; import {By} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms"; import {BlogEntryComponent} from "./blog-entry/blog-entry.component";;
describe('Blog Component', () => { describe('Blog Component', () => {
describe('Isolated Class Test', () => { describe('Isolated Component Test', () => {
let blogComponent: BlogComponent; let blogComponent;
beforeEach(() => { beforeEach(() => {
blogComponent = new BlogComponent(null, null, null); TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([])],
declarations: [BlogComponent]
}); });
});
beforeEach(async(()=> {
TestBed.overrideComponent(BlogComponent, {set: {template: ''}});
blogComponent = TestBed.createComponent(BlogComponent).componentInstance;
}));
it('should have initial entries', () => { it('should have initial entries', () => {
expect(blogComponent.entries.length).toBe(2); expect(blogComponent.entries.length).toBe(2);
@ -102,4 +110,28 @@ describe('Blog Component', () => {
}); });
describe('Basic DOM Form Test including BlogEntryComponent ', () => {
let fixture;
let instance;
let element;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([])],
declarations: [BlogComponent, BlogEntryComponent],
});
fixture = TestBed.createComponent(BlogComponent);
instance = fixture.componentInstance;
element = fixture.debugElement;
});
it('should find first entry title in DOM', () => {
fixture.detectChanges();
const firstEntryTitle = instance.entries[0].title;
expect(element.query(By.css('#entry-list > blog-entry > div > div:nth-child(2) > .title'))
.nativeElement.textContent).toBe(firstEntryTitle);
});
})
}); });

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,7 @@ import {LoginComponent} from "./login.component";
import {RouterTestingModule} from "@angular/router/testing"; import {RouterTestingModule} from "@angular/router/testing";
import {LoginService} from "../services/login-service/login-service"; import {LoginService} from "../services/login-service/login-service";
import {FormsModule} from "@angular/forms"; import {FormsModule} from "@angular/forms";
import {MockEmptyClass} from "../mocks/mock-empty-class"; import {MockLoginService} from "../mocks/mock-login-service";
import Spy = jasmine.Spy; import Spy = jasmine.Spy;
@ -14,7 +14,7 @@ describe('Login Component UI Driven Form Test with Spy', () => {
imports: [FormsModule, RouterTestingModule.withRoutes([])], imports: [FormsModule, RouterTestingModule.withRoutes([])],
declarations: [LoginComponent], declarations: [LoginComponent],
providers: [ providers: [
{provide: LoginService, useClass: MockEmptyClass} {provide: LoginService, useClass: MockLoginService}
], ],
}) })
}); });
@ -30,9 +30,10 @@ describe('Login Component UI Driven Form Test with Spy', () => {
loginFixture.autoDetectChanges(true); loginFixture.autoDetectChanges(true);
// Login ausfuellen und absenden // Login ausfuellen und absenden
loginFixture.whenStable().then(() => {
let service = loginFixture.debugElement.injector.get(LoginService);
// Spy anmelden, die eigentliche Methode wird nicht mehr aufgerufen // Spy anmelden, die eigentliche Methode wird nicht mehr aufgerufen
const spy = spyOn(loginInstance, 'login'); const spy = spyOn(service, 'login');
// Formular Eingabe // Formular Eingabe
loginElement.querySelector('div /deep/ div > #inputEmail').value = inputUser; loginElement.querySelector('div /deep/ div > #inputEmail').value = inputUser;
@ -46,5 +47,3 @@ describe('Login Component UI Driven Form Test with Spy', () => {
}); });
}); });
});

View File

@ -1,3 +0,0 @@
export class MockEmptyClass {
}

View File

@ -0,0 +1,5 @@
export class MockLoginService {
login(name, password) {};
}

View File

@ -18,28 +18,6 @@ import {generateRandomString} from "../../test/test.helper";
describe('EditTask Component', () => { describe('EditTask Component', () => {
@Component({
template: '<router-outlet></router-outlet>'
})
class TestComponent {
}
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule, RouterTestingModule.withRoutes([
{path: 'new', component: EditTaskComponent},
{path: 'edit/:id', component: EditTaskComponent}
],
)],
declarations: [EditTaskComponent, ShowErrorComponent, APPLICATION_VALIDATORS, TestComponent],
providers: [
Title,
{provide: TaskService, useClass: MockTaskService},
]
});
});
describe('Template Driven Form API-based Test', () => { describe('Template Driven Form API-based Test', () => {
let fixture; let fixture;
@ -47,11 +25,20 @@ describe('EditTask Component', () => {
let form; let form;
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule, RouterTestingModule.withRoutes([],
)],
declarations: [EditTaskComponent, ShowErrorComponent, APPLICATION_VALIDATORS],
providers: [
Title,
{provide: TaskService, useClass: MockTaskService},
]
});
fixture = TestBed.createComponent(EditTaskComponent); fixture = TestBed.createComponent(EditTaskComponent);
fixture.autoDetectChanges(true); fixture.autoDetectChanges(true);
}); });
it('should validate the title correctly', (done) => { it('should validate the title correctly', () => {
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
form = fixture.componentInstance.form.form; form = fixture.componentInstance.form.form;
const titleControl = form.get('title'); const titleControl = form.get('title');
@ -66,7 +53,6 @@ describe('EditTask Component', () => {
titleControl.setValue(generateRandomString(101)); titleControl.setValue(generateRandomString(101));
expect(titleControl.errors['maxlength']).toEqual({requiredLength: 100, actualLength: 101}); expect(titleControl.errors['maxlength']).toEqual({requiredLength: 100, actualLength: 101});
done();
}); });
}); });
@ -86,7 +72,7 @@ describe('EditTask Component', () => {
}); });
}); });
it('should show error on tag name provided with gt 0 and lt 3', (done) => { it('should show error on tag name provided with gt 0 and lt 3', () => {
fixture.componentInstance.addTag(); fixture.componentInstance.addTag();
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
form = fixture.componentInstance.form.form; form = fixture.componentInstance.form.form;
@ -99,11 +85,11 @@ describe('EditTask Component', () => {
tagControl.setValue('abc'); tagControl.setValue('abc');
expect(tagControl.errors).toBeNull(); expect(tagControl.errors).toBeNull();
done();
}); });
}); });
it('should validate assignee email correctly', (done) => { it('should validate assignee email correctly', () => {
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
form = fixture.componentInstance.form.form; form = fixture.componentInstance.form.form;
const invalidEmailObject = {invalidEMail: true}; const invalidEmailObject = {invalidEMail: true};
@ -121,16 +107,40 @@ describe('EditTask Component', () => {
assigneeEmail.setValue('test@web.de'); assigneeEmail.setValue('test@web.de');
expect(assigneeEmail.errors).toBeNull(); expect(assigneeEmail.errors).toBeNull();
done();
}) })
}); });
}); });
/**
* Hinweis: Die folgende TestSuite entstammt dem ursprünglichen edit-task.component.spec.ts Tests
* dieses Projektes und ist geistiges Eigentum vom Chritoph Höller, dem Ersteller des Ausgangsprojektes
*/
describe('Routing', () => { describe('Routing', () => {
@Component({
template: '<router-outlet></router-outlet>'
})
class TestComponent {
}
let taskService: TaskService; let taskService: TaskService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule, RouterTestingModule.withRoutes([
{path: 'new', component: EditTaskComponent},
{path: 'edit/:id', component: EditTaskComponent}
],
)],
declarations: [EditTaskComponent, ShowErrorComponent, APPLICATION_VALIDATORS, TestComponent],
providers: [
Title,
{provide: TaskService, useClass: MockTaskService},
]
});
});
beforeEach(inject([TaskService], (_taskService) => { beforeEach(inject([TaskService], (_taskService) => {
taskService = _taskService; taskService = _taskService;
})); }));

View File

@ -1,6 +1,8 @@
// Test Suite // Test Suite
describe('Basic Test', () => { describe('Basic Test', () => {
// geschachtelte TestSuite
describe('Matcher example', () => {
// Variablendeklaration zur Verwendung in mehreren Testfällen // Variablendeklaration zur Verwendung in mehreren Testfällen
let someInt; let someInt;
@ -33,5 +35,45 @@ describe('Basic Test', () => {
it('someInt should have overwritten value from beforeEach()', () => { it('someInt should have overwritten value from beforeEach()', () => {
expect(someInt).toBe(2); expect(someInt).toBe(2);
}); });
});
describe('Calculator Class Testing', () => {
class Calculator {
private val1: number = 0;
private val2: number = 0;
constructor(val1: number, val2: number) {
this.val1 = val1;
this.val2 = val2;
}
add(): number {
return this.val1 + this.val2
};
sub(): number {
return this.val1 - this.val2
};
mul(): number {
return this.val1 * this.val2
};
div(): number {
return this.val1 / this.val2
};
}
it('should return calculated values for each method', () => {
const calc = new Calculator(4, 12);
expect(calc.add()).toBe(16);
expect(calc.sub()).toBe(-8);
expect(calc.mul()).toBe(48);
expect(calc.div()).toBe(1 / 3);
});
});
}); });