Html 为什么赢了';当我附加css文件时,角度组件的t测试通过了吗?
我正在尝试测试Angular 4中的一个组件,它有一个外部模板和一个css文件。我正在使用一个异步beforeach来使用TestBed编译组件,但除非我删除css文件的内容并从html标记中删除类和id,否则它不会工作。为什么会这样 以下是规格:Html 为什么赢了';当我附加css文件时,角度组件的t测试通过了吗?,html,unit-testing,angular,phantomjs,karma-jasmine,Html,Unit Testing,Angular,Phantomjs,Karma Jasmine,我正在尝试测试Angular 4中的一个组件,它有一个外部模板和一个css文件。我正在使用一个异步beforeach来使用TestBed编译组件,但除非我删除css文件的内容并从html标记中删除类和id,否则它不会工作。为什么会这样 以下是规格: import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; describe('AppCompon
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
}).compileComponents();
}));
it('should create the app', async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title 'app works!'`, async(() => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('app works!');
}));
it('should render title in a h1 tag', async(() => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain('appworks!');
}));
});
以下是组件:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app works!';
}
以下是html页面:
<h1>
{{title}}
</h1
每次测试都会出现相同的错误。
任何帮助都将不胜感激。我被阻止了好几天。也许这与视图封装有关,当您为组件指定
style
或styleurl
时,angular应用了视图封装。在这种情况下,默认情况下angular模拟阴影dom。尝试将以下内容添加到您的@组件
对象:封装:ViewEncapsulation.None
并查看它是否有助于您使用angular cli?是的,我正在使用angular-cli@Riiverside这就解决了问题。你能解释一下原因吗?这对我来说更像是个虫子。我看不出为什么默认视图封装在测试期间会产生错误。您可能希望在angular github上打开一个问题,可能这与angular在为组件指定样式
或样式URL
时应用的视图封装有关。在这种情况下,默认情况下angular模拟阴影dom。尝试将以下内容添加到您的@组件
对象:封装:ViewEncapsulation.None
并查看它是否有助于您使用angular cli?是的,我正在使用angular-cli@Riiverside这就解决了问题。你能解释一下原因吗?这对我来说更像是个虫子。我看不出为什么默认视图封装在测试期间会产生错误。您可能希望在github回购协议上发行一个问题
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}