【发布时间】:2019-07-23 11:54:40
【问题描述】:
我想为一个组件编写单元测试。第一次测试失败, 出现此错误:
错误:预期未定义是真实的。
it 块输出错误:
it('should create', () => {
expect(component).toBeTruthy();
});
登录模板:
<h3>Login</h3>
<form class="form form-group" (ngSubmit)="onSubmit()">
<div class="row">
<label for="email" class="login-form-label col-4">Email:</label>
<input ngModel [(ngModel)]="email" name="email" (ngModelChange)="validateEmail()" type="email" id="email" class="col-3 form-control">
<span class="error col-sm-4">{{ this.emailErr }}</span>
</div>
<br>
<div class="row">
<label for="password" class="login-form-label col-4">Wachtwoord:</label>
<input ngModel [(ngModel)]="password" name="password" (ngModelChange)="validatePassword()" type="password" id="password" class="col-3 form-control">
<span class="error col-sm-4">{{ this.passwordErr }}</span>
</div>
<input type="submit" [disabled]="!isValid()" value="Login" class="login-button col-1">
</form>
我试过了:
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
providers: [
LoginComponent,
{ provide: RoutingService, useValue: MockRoutingService },
{ provide: AuthenticationService, useValue: MockAuthenticationService }
]
})
.compileComponents();
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
});
还有:
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
providers: [
LoginComponent,
{ provide: RoutingService, useValue: MockRoutingService },
{ provide: AuthenticationService, useValue: MockAuthenticationService }
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
还有:
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
providers: [
LoginComponent,
{ provide: RoutingService, useValue: MockRoutingService },
{ provide: AuthenticationService, useValue: MockAuthenticationService }
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
LoginComponent:
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
private password = '';
private email = '';
private emailErr = '';
private passwordErr = '';
constructor(private authService: AuthenticationService, private routingService: RoutingService) { }
ngOnInit() {
}
onSubmit() {
this.emailErr = '';
this.passwordErr = '';
const response: { ['emailValid']: boolean, ['passwordValid']: boolean } = this.authService.login(this.email, this.password);
const self = this;
setTimeout(() => {
if (response.emailValid === false) {
self.emailErr = '* Your username was incorrect, please try again.';
return;
} else if (response.passwordValid === false) {
self.passwordErr = '* Your password was incorrect, please try again.';
return;
}
self.routingService.route('home');
}, 300);
}
validateEmail() {
this.emailErr = '';
if (this.email === '') {
this.emailErr = '* Please enter your email.';
}
}
validatePassword() {
this.passwordErr = '';
if (this.password === '') {
this.passwordErr = '* Please enter your password.';
}
}
isValid() {
if (this.password === '' || this.email === '') {
return false;
} else if (this.emailErr !== '' || this.passwordErr !== '') {
return false;
}
return true;
}
}
我在这里错过了什么?
【问题讨论】:
-
尝试减少代码示例并提出更具体的问题。这是很多要审查的来源
-
@Gnqz 我减少了它,但不确定我是否应该将登录组件保留在帖子中。
-
错误是在第一个
it块内断言组件已定义?正如在下面的答案中所说,您肯定需要从提供程序列表中删除 LoginComponent 并在声明您的 TestBed 配置的 beforeEach 中使用 async 。您能否分享失败的规范中的测试用例(it块)和您的 LoginComponent 模板(html)? -
@Erbsenkoenig 刚刚更新了帖子,希望对您有所帮助
标签: angular unit-testing