이 글을 읽은 후, 단지 2 개의 입력 상자와 제출 버튼이있는 간단한 로그인 페이지를 테스트하기로 결정했습니다. 그런 다음이 구성 요소는 LoginService
을 사용하여 이러한 데이터를 백엔드에 전달합니다.서비스로 구성 요소를 테스트 할 수 없습니다.
(는 또한 내가 같은 단위 테스트에 새로 온 점에 유의, 그래서 나는이 같은 구성 요소를 테스트하는 방법으로 좋은 방법이 있는지 확실하지 않습니다.) 우선 들어
, 난 단지 원 #username
입력 요소의 초기 값이 비어 있는지 확인하십시오. 하지만 심지어 사양으로 인해 아래에보고 된 문제에 작업 할 수 없습니다 :
Chrome 55.0.2883 (Windows 7 0.0.0) LoginComponent Username field should be empty FAILED
Error: DI Error
Error: Uncaught (in promise): Error: No provider for Http!
TypeError: Cannot read property 'detectChanges' of undefined
Chrome 55.0.2883 (Windows 7 0.0.0): Executed 4 of 4 (1 FAILED) (0 secs/0.456 secs)
login.component :
Chrome 55.0.2883 (Windows 7 0.0.0) LoginComponent Username field should be empty FAILED
Failed: Unexpected value 'Http' imported by the module 'DynamicTestModule'
Error: Unexpected value 'Http' imported by the module 'DynamicTestModule'
TypeError: Cannot read property 'detectChanges' of undefined
Chrome 55.0.2883 (Windows 7 0.0.0): Executed 4 of 4 (1 FAILED) (0 secs/0.348 secs)
내가 HTTP 모듈을 삭제했을 때,이 오류가 발생했습니다. HTML
<div class="login jumbotron center-block">
<h1>Login</h1>
<form (ngSubmit)="onSubmit($event)" #loginForm="ngForm">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" [(ngModel)]="model.username" name="username"
placeholder="Username" #username="ngModel" required>
<div [hidden]="username.valid || username.pristine" class="alert alert-danger"> Username is required </div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" [(ngModel)]="model.password" name="password" placeholder="Password" #password="ngModel" required>
<div [hidden]="password.valid || password.pristine" class="alert alert-danger"> Password is required </div>
</div>
<button type="submit" class="btn btn-default" [disabled]="!loginForm.form.valid" >Submit</button>
<a [routerLink]="['/signup']">Click here to Signup</a>
</form>
</div>
login.component.ts
,536,913,632 10import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { LoginService } from '../services/login.service';
import { User } from '../extensions/user.class';
@Component({
moduleId: module.id,
selector: 'login',
templateUrl: '../templates/login.component.html',
styleUrls: [ '../styles/login.component.css' ],
providers: [ LoginService ]
})
export class LoginComponent {
private submitted = false;
private model = new User();
constructor(
private router: Router,
private loginService: LoginService
) {}
public onSubmit(event: any): void {
event.preventDefault();
if (! this.submitted) {
this.submitted = true;
if (this.model.username && this.model.password) {
this.loginService.login(this.model).then((token) => {
localStorage.setItem('id_token', token.id);
this.router.navigate(['home']);
}).catch((error) => this.onLoginFailed(error));
} else {
console.warn('No username or password provided');
}
}
}
private onLoginFailed(error: any): void {
//// errors are already handled in login-service ////
console.error(error);
this.submitted = false; /// reset form submit funcitonality ///
}
public signup(event: any): void {
event.preventDefault();
this.router.navigate(['signup']);
}
}
login.component.spec.ts
import { async } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { LoginComponent } from './login.component';
import { LoginService } from '../services/login.service';
import { Http } from '@angular/http';
import { User } from '../extensions/user.class';
@Component({
template: ''
})
class DummyComponent{}
class LoginServiceStub {
login(user: User){
return true;
}
}
describe('LoginComponent',() => {
let comp: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let de: DebugElement;
let el: HTMLElement;
let location: Location;
// async beforeEach
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent, DummyComponent ], // declare the test component
providers: [
{ provide: LoginService, useClass: LoginServiceStub }
],
imports: [
FormsModule ,
RouterTestingModule.withRoutes([
{ path: 'singup', component: DummyComponent }
])
]
}).compileComponents() // compile template and css
.then(() => {
fixture = TestBed.createComponent(LoginComponent);
comp = fixture.componentInstance; // LoginComponent test instance
de = fixture.debugElement.query(By.css('input[name="username"]'));
el = de.nativeElement;
});
}));
it('Username field should be empty',() => {
fixture.detectChanges();
expect(el.textContent).toContain('');
});
});