问题描述
我有一个简单的angular
应用程序,其中包含AppComponent
和ProductComponent
.当我运行ng test
命令时,在http://localhost:9876/?id=80860281
I have simple angular
application with AppComponent
and ProductComponent
. When I run ng test
command, getting below error on http://localhost:9876/?id=80860281
Jesmine 3.5.0Options
finished in 0.473s
4 specs, 1 failure, randomized with seed 16102
Spec List | Failures
AppComponent > should render title
TypeError: Cannot read property 'textContent' of null
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:33:51)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:364:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:292:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:363:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:123:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:545:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:560:1)
at <Jasmine>
这是我的代码
app.module.ts
类
@NgModule({
declarations: [
AppComponent,
ProductComponent
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]})
app.component.ts
类
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Product';
}
文件app.component.spec.ts
和product.component.spec.ts
具有默认代码.我没有修改那些文件.
files app.component.spec.ts
and product.component.spec.ts
have the default code. I didn't modified those files.
app.component.spect.ts
文件代码
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'Assignment01`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('Assignment01');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('.title').textContent).toContain('Product app is running!');
});
});
product.component.ts
文件
export class ProductComponent implements OnInit {
constructor() { }
productName:any
quantity:any
public data:any[]=[
{
"ID": 101,
"Product": "Moto G",
"Quantity": 2,
},
{
"ID": 102,
"Product": "TV",
"Quantity": 4,
},
{
"ID": 105,
"Product": "Watch",
"Quantity": 2,
},
]
ngOnInit(): void {}
addItem() {
this.data.push({ Product: this.productName, Quantity: this.quantity});
}
product.component.html
文件
<div>
<h1>Demonstration of Built in directives and data binding</h1>
<br>
<h2>Product List</h2>
<table border="1" *ngIf='data && data.length'>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let record of data'>
<td>{{record.Product}}</td>
<td>{{record.Quantity}}</td>
</tr>
</tbody>
</table>
<br>
<label>Enter product name:</label>
<input type="text" [(ngModel)]="productName" />
<label>Enter quantity:</label>
<input type="text" [(ngModel)]="quantity"/>
<button (click)='addItem()'>Add</button>
<br/><br/>
</div>
app.component.html
文件只有<router-outlet></router-outlet>
.
如何解决此问题?
推荐答案
根据输出,您的HTML需要具有
As per the output, your HTML need to have
<div class="content"><span>Product app is running!</span> </div>
使其正常工作.
这篇关于无法读取空Angular Testing的属性'textContent'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!