我一直在测试我的登录模块,但订阅没有得到测试,我也无法检查本地存储。到目前为止,我是初学者,我已经这样做了
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import {MatSelectModule, MatInputModule, MatCardModule, MatButtonModule,MatDatepickerModule, MatNativeDateModule } from '@angular/material';
import { FormGroup, FormsModule, FormControl, FormBuilder, Validators,ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import {HttpClientModule} from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { LoginComponent } from './login.component';
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
imports: [ RouterTestingModule,ReactiveFormsModule , FormsModule ,MatSelectModule, HttpClientModule, MatInputModule, BrowserAnimationsModule, MatCardModule, MatButtonModule,MatDatepickerModule, MatNativeDateModule],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
这是测试覆盖率
login.component.ts -edit 添加登录组件
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormsModule, FormControl, FormBuilder, Validators } from '@angular/forms';
import { LoginService } from '../../services/login.service';
import { Router } from '@angular/router'
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginUserData = {}
loginForm: FormGroup;
constructor(private _login: LoginService, private _router: Router, private loginFormBuilder: FormBuilder) {
this.createForm();
}
ngOnInit() {
}
createForm() {
this.loginForm = this.loginFormBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
}
loginUser() {
this._login.loginUser(this.loginUserData).subscribe(
res => {
localStorage.setItem('token', res.token)
this._router.navigate(['/home'])
},
err => console.log(err))
}
}
登录 html- 编辑添加的登录 html 页面
<mat-card class="login-main">
<mat-card-title class="login-title">
<span class="welcome">Welcome back</span>
<img src="assets/logo.png" class="image-log" alt="logo">
</mat-card-title>
<mat-card-content class="login-content">
<form [formGroup]="loginForm">
<mat-form-field class="input-username">
<input matInput formControlName="username" placeholder="Username" name="username" type="text" [(ngModel)]="loginUserData.username">
</mat-form-field>
<div *ngIf="loginForm.controls['username'].invalid && (loginForm.controls['username'].dirty || loginForm.controls['username'].touched)">
<div class="error-text" *ngIf="loginForm.controls['username'].errors.required">
Please enter the username.
</div>
</div>
<mat-form-field class="input-password">
<input matInput formControlName="password" placeholder="Password" name="password" type="password" [(ngModel)]="loginUserData.password">
</mat-form-field>
<div *ngIf="loginForm.controls['password'].invalid && (loginForm.controls['password'].dirty || loginForm.controls['password'].touched)">
<div class="error-text" *ngIf="loginForm.controls['password'].errors.required">
Please enter the password.
</div>
</div>
<a routerLink="/forgotpassword" class="forgot-password">Forgot password?</a>
<br>
<button class="login-button" type="submit" (click)="loginUser()" [disabled]='loginForm.status =="INVALID"' mat-Button>LOGIN</button>
</form>
</mat-card-content>
</mat-card>
任何帮助,将不胜感激
谢谢