title: Angular Material 2 MdMenu unit test case failing. Error: Cannot read property 'unsubscribe' of undefined
body: <p>I am using angular-cli to create my component and working with Material2.</p>
  
  <p>In test.component.html</p>
  
  <pre><code>&lt;md-toolbar&gt;
  &lt;button md-icon-button [mdMenuTriggerFor]="anchorMenu" aria-label="Open anchor menu"&gt;
    &lt;md-icon&gt;more_vert&lt;/md-icon&gt;
  &lt;/button&gt;
  &lt;/md-toolbar&gt;
  
  &lt;md-menu #anchorMenu="mdMenu"&gt; //getting error on this line
  &lt;a md-menu-item *ngFor="let item of items" href="http://www.google.com" [disabled]="item.disabled"&gt;
    {{ item.text }}
  &lt;/a&gt;
  &lt;/md-menu&gt;
  </code></pre>
  
  <p>While Unit Testing:</p>
  
  <blockquote>
    <p>Error: Error in ./TestCompComponent class TestCompComponent - inline
    template:7:4 caused by: Cannot read property 'unsubscribe' of
    undefined</p>
  </blockquote>
  
  <p>Everyhing works fine when I run <strong>ng serve</strong> but getting above error on <strong>ng test</strong></p>
  
  <p>In test.component.spec.ts</p>
  
  <pre><code>import { async, ComponentFixture, TestBed } from '@angular/core/testing';
  import 'hammerjs';
  import { MaterialModule } from '@angular/material';
  import { TestComponent } from './test.component';
  
  describe('TestComponent', () =&gt; {
    let component: TestComponent;
    let fixture: ComponentFixture&lt;TestComponent&gt;;
  
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        imports: [MaterialModule],
        declarations: [TestComponent]
      })
        .compileComponents();
    }));
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(TestComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
  
    it('should create', () =&gt; {
      expect(component).toBeTruthy();
    });
  });
  </code></pre>
  
  <p>Some answers said "fixture.detectChanges();" will work but this is already present and not working for me. Some more answers said need to add Observable but I am not using any service. This is simple MdMenu from Material2.</p>
  
  <p>Debugging:</p>
  
  <ol>
  <li>When I remove [mdMenuTriggerFor]="anchorMenu" and md-menu reference
  I am not getting any error in Unit testing.</li>
  <li>When I am removing "ui-test-comp" tag from
  app.component.html I am not getting this error and all test cases
  are getting pass.</li>
  </ol>
  
  <p>Please find plnkr link for all related file reference. (Please note that this is not working plnkr and just for reference purpose.)</p>
  
  <p><a href="https://plnkr.co/edit/AFzHWG28ZYgLZTs5aZgF" rel="nofollow noreferrer">https://plnkr.co/edit/AFzHWG28ZYgLZTs5aZgF</a></p>