gpt4 book ai didi

unit-testing - Angular 2 和 Jasmine 单元测试 : cannot get the innerHtml

转载 作者:太空狗 更新时间:2023-10-29 17:12:35 25 4
gpt4 key购买 nike

我正在使用测试组件“WelcomeComponent”的示例之一:

import { Component, OnInit } from '@angular/core';
import { UserService } from './model/user.service';

@Component({
selector: 'app-welcome',
template: '<h3>{{welcome}}</h3>'
})
export class WelcomeComponent implements OnInit {
welcome = '-- not initialized yet --';
constructor(private userService: UserService) { }

ngOnInit(): void {
this.welcome = this.userService.isLoggedIn ?
'Welcome ' + this.userService.user.name :
'Please log in.';
}
}

这是测试用例,我正在检查“h3”是否包含用户名“Bubba”:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { UserService } from './model/user.service';
import { WelcomeComponent } from './welcome.component';


describe('WelcomeComponent', () => {

let comp: WelcomeComponent;
let fixture: ComponentFixture<WelcomeComponent>;
let componentUserService: UserService; // the actually injected service
let userService: UserService; // the TestBed injected service
let de: DebugElement; // the DebugElement with the welcome message
let el: HTMLElement; // the DOM element with the welcome message

let userServiceStub: {
isLoggedIn: boolean;
user: { name: string }
};

beforeEach(() => {
// stub UserService for test purposes
userServiceStub = {
isLoggedIn: true,
user: { name: 'Test User' }
};

TestBed.configureTestingModule({
declarations: [WelcomeComponent],
// providers: [ UserService ] // NO! Don't provide the real service!
// Provide a test-double instead
providers: [{ provide: UserService, useValue: userServiceStub }]
});

fixture = TestBed.createComponent(WelcomeComponent);
comp = fixture.componentInstance;

// UserService actually injected into the component
userService = fixture.debugElement.injector.get(UserService);
componentUserService = userService;
// UserService from the root injector
userService = TestBed.get(UserService);
// get the "welcome" element by CSS selector (e.g., by class name)
el = fixture.debugElement.nativeElement; // de.nativeElement;
});


it('should welcome "Bubba"', () => {
userService.user.name = 'Bubba'; // welcome message hasn't been shown yet
fixture.detectChanges();
const content = el.querySelector('h3');
expect(content).toContain('Bubba');
});
});

当使用 Karma 测试和调试测试用例时,如果我在控制台中评估“el.querySelector('h3'),它会显示以下内容

<h3>Welcome  Bubba</h3>

如何,我可以获得标题的 innerHtml,因为将它包含在 ts 文件中时它不会解析,并且测试用例的计算结果始终为 false。

enter image description here

这就是它所说的:“innerHTML”在类型“HTMLHeadingElement”上不存在

最佳答案

是因为content

const content = el.querySelector('h3');
expect(content).toContain('Bubba');

HTMLNode ,而不是原始文本。所以你期待一个 HTMLNode成为一个字符串。这将失败。

您需要使用 content.innerHTML 提取原始 HTML或 content.textContent (只获取 <h3> 标签之间的内容

const content = el.querySelector('h3');
expect(content.innerHTML).toContain('Bubba');
expect(content.textContent).toContain('Bubba');

关于unit-testing - Angular 2 和 Jasmine 单元测试 : cannot get the innerHtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40227533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com