ng test와 ng e2e 적용 방법과 차이점을 알아봅시다.
목적: 개별 컴포넌트, 서비스, 파이프 등 내부 로직과 의존성 주입(DI)을 검증합니다.
(업데이트)
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from '../app/app.component';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('app component를 생성합니다.', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`타이틀은 'app title' 입니다.`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app title');
}));
it(`'컨텐츠는 'app content' 입니다.`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const app = fixture.debugElement.componentInstance;
expect(app.content).toEqual('app content');
}));
it(`h1 태그에 들어갈 제목은 'Welcome to app title' 입니다.`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('Welcome to app title!');
}));
it(`로그인 정보를 입력합니다`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
// fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
compiled.querySelector('input#email').value = 'test@test.com';
compiled.querySelector('input#password').value = 'password';
compiled.querySelector('button.btn').click();
}));
});
(업데이트)
import { browser, by, element, $ } from 'protractor';
export class AppPage {
navigateTo() {
return browser.get('/');
}
// getParagraphText() {
// return element(by.css('app-root h1')).getText();
// }
getTitle() {
return $('h1').getText();
}
getContent() {
return $('h3').getText();
}
loginTest() {
$('input#email').sendKeys('test@test.com');
$('input#password').sendKeys('password');
$('button.btn').click();
}
}
import { AppPage } from './app.po';
describe('workspace-project App', () => {
let page: AppPage;
beforeEach(() => {
page = new AppPage();
});
it('제목 표시 여부', () => {
page.navigateTo();
expect(page.getTitle()).toEqual('Welcome to app title!');
});
it('로그인', () => {
page.navigateTo();
expect(page.loginTest());
});
});