angular2测试,我如何模拟子组件

all*_*kim 52 testing components unit-testing jasmine angular

如何在茉莉花测试中模拟子组件?

我有MyComponent,使用MyNavbarComponentMyToolbarComponent

import {Component} from 'angular2/core';
import {MyNavbarComponent} from './my-navbar.component';
import {MyToolbarComponent} from './my-toolbar.component';

@Component({
  selector: 'my-app',
  template: `
    <my-toolbar></my-toolbar>
    {{foo}}
    <my-navbar></my-navbar>
  `,
  directives: [MyNavbarComponent, MyToolbarComponent]
})
export class MyComponent {}
Run Code Online (Sandbox Code Playgroud)

当我测试这个组件时,我不想加载和测试这两个子组件; MyNavbarComponent,MyToolbarComponent,所以我想模仿它.

我知道如何使用服务进行模拟provide(MyService, useClass(...)),但我不知道如何模拟指令; 组件;

  beforeEach(() => {
    setBaseTestProviders(
      TEST_BROWSER_PLATFORM_PROVIDERS,
      TEST_BROWSER_APPLICATION_PROVIDERS
    );

    //TODO: want to mock unnecessary directives for this component test
    // which are MyNavbarComponent and MyToolbarComponent
  })

  it('should bind to {{foo}}', injectAsync([TestComponentBuilder], (tcb) => {
    return tcb.createAsync(MyComponent).then((fixture) => {
      let DOM = fixture.nativeElement;
      let myComponent = fixture.componentInstance;
      myComponent.foo = 'FOO';
      fixture.detectChanges();
      expect(DOM.innerHTML).toMatch('FOO');
    });
  });
Run Code Online (Sandbox Code Playgroud)

这是我的掠夺者例子;

http://plnkr.co/edit/q1l1y8?p=preview

小智 61

根据要求,我发布了另一个关于如何用input/ 模拟子组件的答案output:

所以让我们首先说我们有TaskListComponent显示任务,并在点击其中一个时刷新:

<div id="task-list">
  <div *ngFor="let task of (tasks$ | async)">
    <app-task [task]="task" (click)="refresh()"></app-task>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

app-task是具有[task] 输入和(click) 输出的子组件.

好的,现在我们想为我编写测试TaskListComponent,当然我们不想测试真正的app-task组件.

所以@Klas建议我们可以配置我们TestModule:

schemas: [CUSTOM_ELEMENTS_SCHEMA]
Run Code Online (Sandbox Code Playgroud)

我们可能在构建或运行时都没有出现任何错误,但除了子组件的存在之外,我们将无法进行更多测试.

那么我们如何模拟子组件呢?

首先,我们将为子组件(相同的选择器)定义一个模拟指令:

@Directive({
  selector: 'app-task'
})
class MockTaskDirective {
  @Input('task')
  public task: ITask;
  @Output('click')
  public clickEmitter = new EventEmitter<void>();
}
Run Code Online (Sandbox Code Playgroud)

现在我们将在测试模块中声明它:

let fixture : ComponentFixture<TaskListComponent>;
let cmp : TaskListComponent;

beforeEach(() => {
  TestBed.configureTestingModule({
    declarations: [TaskListComponent, **MockTaskDirective**],
    // schemas: [CUSTOM_ELEMENTS_SCHEMA],
    providers: [
      {
        provide: TasksService,
        useClass: MockService
      }
    ]
  });

  fixture = TestBed.createComponent(TaskListComponent);
  **fixture.autoDetectChanges();**
  cmp = fixture.componentInstance;
});
Run Code Online (Sandbox Code Playgroud)
  • 请注意,由于夹具的子组件的生成在创建后异步发生,因此我们激活其autoDetectChanges功能.

在我们的测试中,我们现在可以查询指令,访问其DebugElement的注入器,并通过它获取我们的模拟指令实例:

import { By } from '@angular/platform-browser';    
const mockTaskEl = fixture.debugElement.query(By.directive(MockTaskDirective));
const mockTaskCmp = mockTaskEl.injector.get(MockTaskDirective) as MockTaskDirective;
Run Code Online (Sandbox Code Playgroud)

[这部分通常应该放在beforeEach部分中,以获得更清晰的代码.]

从这里开始,测试是小菜一碟:)

it('should contain task component', ()=> {
  // Arrange.
  const mockTaskEl = fixture.debugElement.query(By.directive(MockTaskDirective));

  // Assert.
  expect(mockTaskEl).toBeTruthy();
});

it('should pass down task object', ()=>{
  // Arrange.
  const mockTaskEl = fixture.debugElement.query(By.directive(MockTaskDirective));
  const mockTaskCmp = mockTaskEl.injector.get(MockTaskDirective) as MockTaskDirective;

  // Assert.
  expect(mockTaskCmp.task).toBeTruthy();
  expect(mockTaskCmp.task.name).toBe('1');
});

it('should refresh when task is clicked', ()=> {
  // Arrange
  spyOn(cmp, 'refresh');
  const mockTaskEl = fixture.debugElement.query(By.directive(MockTaskDirective));
  const mockTaskCmp = mockTaskEl.injector.get(MockTaskDirective) as MockTaskDirective;

  // Act.
  mockTaskCmp.clickEmitter.emit();

  // Assert.
  expect(cmp.refresh).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)

  • @Alex我想知道同样的事情..来自'@ angular/platform-b​​rowser'的`import {By};`为我工作. (3认同)

Kla*_*urn 24

如果您使用schemas: [CUSTOM_ELEMENTS_SCHEMA]TestBed组件测试将不会加载子组件.

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { TestBed, async } from '@angular/core/testing';
import { MyComponent } from './my.component';

describe('App', () => {
  beforeEach(() => {
    TestBed
      .configureTestingModule({
        declarations: [
          MyComponent
        ],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
      });
  });

  it(`should have as title 'app works!'`, async(() => {
    let fixture = TestBed.createComponent(MyComponent);
    let app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('Todo List');
  }));

});
Run Code Online (Sandbox Code Playgroud)

这适用于Angular 2.0的发布版本. 这里的完整代码样本.

替代CUSTOM_ELEMENTS_SCHEMANO_ERRORS_SCHEMA


all*_*kim 7

感谢Eric Martinez,我找到了这个解决方案.

我们可以使用overrideDirective这里记录的函数, https://angular.io/docs/ts/latest/api/testing/TestComponentBuilder-class.html

它需要三个prarmeters; 1.要实现的组件2.要覆盖的子组件3.模拟组件

已解决的解决方案位于http://plnkr.co/edit/a71wxC?p=preview

这是来自plunker的代码示例

import {MyNavbarComponent} from '../src/my-navbar.component';
import {MyToolbarComponent} from '../src/my-toolbar.component';

@Component({template:''})
class EmptyComponent{}

describe('MyComponent', () => {

  beforeEach(injectAsync([TestComponentBuilder], (tcb) => {
    return tcb
      .overrideDirective(MyComponent, MyNavbarComponent, EmptyComponent)
      .overrideDirective(MyComponent, MyToolbarComponent, EmptyComponent)
      .createAsync(MyComponent)
      .then((componentFixture: ComponentFixture) => {
        this.fixture = componentFixture;
      });
  ));

  it('should bind to {{foo}}', () => {
    let el = this.fixture.nativeElement;
    let myComponent = this.fixture.componentInstance;
    myComponent.foo = 'FOO';
    fixture.detectChanges();
    expect(el.innerHTML).toMatch('FOO');    
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 如何在Angular ^ 2.0.0中使用`TestBed`完成此操作? (5认同)
  • 然而,`TestComponentBuilder` 在新的`TestBed 中被弃用。overrideDirective` 只允许覆盖元数据 (3认同)

Chr*_*ato 5

我整理了一个简单的MockComponent模块,以帮助简化这一过程:

import { TestBed } from '@angular/core/testing';
import { MyComponent } from './src/my.component';
import { MockComponent } from 'ng2-mock-component';

describe('MyComponent', () => {

  beforeEach(() => {

    TestBed.configureTestingModule({
      declarations: [
        MyComponent,
        MockComponent({ 
          selector: 'my-subcomponent', 
          inputs: ['someInput'], 
          outputs: [ 'someOutput' ]
        })
      ]
    });

    let fixture = TestBed.createComponent(MyComponent);
    ...
  });

  ...
});
Run Code Online (Sandbox Code Playgroud)

它可以在 https://www.npmjs.com/package/ng2-mock-component上找到.