Angular Material 2 MdMenu单元测试用例失败。错误:无法读取属性'取消订阅'未定义的

时间:2017-03-08 07:27:56

标签: angular karma-jasmine angular-cli angular-material2

我使用angular-cli创建我的组件并使用Material2。

在test.component.html

<md-toolbar>
<button md-icon-button [mdMenuTriggerFor]="anchorMenu" aria-label="Open anchor menu">
  <md-icon>more_vert</md-icon>
</button>
</md-toolbar>

<md-menu #anchorMenu="mdMenu"> //getting error on this line
<a md-menu-item *ngFor="let item of items" href="http://www.google.com" [disabled]="item.disabled">
  {{ item.text }}
</a>
</md-menu>

单元测试时:

  

错误:./TestCompComponent类中的错误TestCompComponent - 内联   模板:7:4引起:无法读取属性&#39;取消订阅&#39;的   未定义

当我运行 ng serve 时,Everyhing工作正常,但在 ng test

上面出现错误

在test.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import 'hammerjs';
import { MaterialModule } from '@angular/material';
import { TestComponent } from './test.component';

describe('TestComponent', () => {
  let component: TestComponent;
  let fixture: ComponentFixture<TestComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [MaterialModule],
      declarations: [TestComponent]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

一些答案​​说&#34; fixture.detectChanges();&#34;会工作,但这已经存在,不适合我。更多的答案说需要添加Observable,但我没有使用任何服务。这是Material2的简单MdMenu。

调试:

  1. 当我删除[mdMenuTriggerFor] =&#34; anchorMenu&#34;和md菜单参考 我在单元测试中没有收到任何错误。
  2. 当我删除&#34; ui-test-comp&#34;标签来自 app.component.html我没有收到此错误和所有测试用例 正在通过。
  3. 请找到所有相关文件参考的plnkr链接。 (请注意,这不适用于plnkr,仅供参考。)

    https://plnkr.co/edit/AFzHWG28ZYgLZTs5aZgF

0 个答案:

没有答案