微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法在同一个 jasmine 文件中渲染两个 Angular 组件?

如何解决有没有办法在同一个 jasmine 文件中渲染两个 Angular 组件?

我想用两个角度组件进行集成测试,为了做到这一点,我需要同时渲染两者。我们可以这样做吗?是一种好的做法吗?

解决方法

是的,你可以,它会涉及到 this 之类的东西。这可能是个好主意,具体取决于您要做什么。

试试:

import { By } from '@angular/platform-browser';
.....
// put this on top of the describe
@Component({
  template: `
    <component-1
      [hero]="hero" (selected)="onSelected($event)">
    </component-1>
    <component-2></component-2>
   `
})
class TestHostComponent {
  hero: Hero = {id: 42,name: 'Test Name'};
  selectedHero: Hero;
  onSelected(hero: Hero) {
    this.selectedHero = hero;
  }
}

let fixture: ComponentFixture<TestHostComponent>;
let component1: Component1;
let component2: Component2;
....
TestBed.configureTestingModule({
  declarations: [
    TestHostComponent,Component1,Component2,]
});
....
fixture = TestBed.createComponent(TestHostComponent);
testHost = fixture.componentInstance;
// get a handle on component1 and component2
component1 = fixture.debugElement.query(By.directive(Component1)).componentInstance;
component2 = fixture.debugElement.query(By.directive(Component2)).componentInstance;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。