Angular 2和Jasmine单元测试:无法获取innerHtml

Hus*_*man 8 unit-testing jasmine karma-jasmine angular

我正在使用测试组件'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.';
    }
}
Run Code Online (Sandbox Code Playgroud)

这是测试用例,我正在检查'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');
    });
});
Run Code Online (Sandbox Code Playgroud)

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

<h3>Welcome  Bubba</h3>
Run Code Online (Sandbox Code Playgroud)

怎么样,我可以得到标题的innerHtml,因为当它包含在ts文件中并且测试用例总是评估为false时它不会解析.

在此输入图像描述

这就是它所说的''innerHTML'在'HTMLHeadingElement ' 类型上不存在

Pau*_*tha 17

这是因为 content

const content = el.querySelector('h3');
expect(content).toContain('Bubba');
Run Code Online (Sandbox Code Playgroud)

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

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

const content = el.querySelector('h3');
expect(content.innerHTML).toContain('Bubba');
expect(content.textContent).toContain('Bubba');
Run Code Online (Sandbox Code Playgroud)

  • @Coding - re: missing prop --&gt; `const content = el.querySelector('h3') as HTMLElement;` `content` 现在有 `.innerHTML` (2认同)