Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Angular 2中加载外部css并进行测试大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_944_2@
我正在尝试创建一个带有测试的组件.该组件具有我想要测试的外部CSs.我想我做的一切都是正确的,但我无法通过测试.这是我的代码
app.component.spec.ts

import { AppComponent } from "./app.component";
import { async,TESTBed } from "@angular/core/tesTing";
import { By } from "@angular/platform-browser";

describe("App Component",function () {
    beforeEach(async(() => {
        TESTBed.configureTesTingModule({
            declarations: [AppComponent],}).compileComponents()
    }));
    it("should instantiate component",() => {
        let fixture = TESTBed.createComponent(AppComponent);
        expect(fixture.componenTinstance instanceof AppComponent).toBe(true);
    });
    it("should have expected <h1> text",() => {
        let fixture = TESTBed.createComponent(AppComponent);
        fixture.detectChanges();

        let h1 = fixture.debugElement.query(By.css("h1"));

        expect(h1.nativeElement.innerText).toBe("Hello World!");

        expect(h1.nativeElement.style.color).toBe("blue");
    });
});

app.component.ts:

import { Component } from "@angular/core";

@Component({
    modulEID: module.id,SELEctor: "nebula-app",styleUrls: ["./app.component.css"],templateUrl: "./app.component.html",})
export class AppComponent { }

app.component.html:

<h1>Hello World!</h1>

app.component.css:

h1{
    color: blue;
}

只有最后的期望才会失败.它将h1.nativeElement.style.color视为空.好像它没有以某种方式加载CSs.如果我在html中将样式设置为线条样式,则此测试将通过.但是将它作为外部css将会失败.

我究竟做错了什么?我的假设是错误的,compileComponents应该加载外部CSS并将其作为nativeElement的样式?

@H_944_2@

解决方法

h1.nativeElement.style仅包含在元素上显式设置的样式.例如,以下元素将具有BACkgroundColorset为红色的样式对象

<h1 style="BACkground-color: red;">Hello World!</h1>

您可以使用量角器e2e测试对其进行测试,而不是使用颜色的单元测试.你可以写一个这样的测试:

expect(element(by.css('h1')).getCssvalue('color')).toEqual('rgba(0,255,1)');

量角器e2e测试内置于angular-cli生成的Angular2项目中.使用命令ng e2e运行它们

@H_944_2@ @H_944_2@
@H_944_2@
@H_944_2@

大佬总结

以上是大佬教程为你收集整理的在Angular 2中加载外部css并进行测试全部内容,希望文章能够帮你解决在Angular 2中加载外部css并进行测试所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。