title: Angular2 Inject ElementRef in unit test
body: <p>I am trying to test a component that receives a reference to ElementRef through DI. </p>
  
  <pre class="lang-ts prettyprint-override"><code>import { Component, OnInit, ElementRef } from '@angular/core';
  
  @Component({
    selector: 'cp',
    templateUrl: '...',
    styleUrls: ['...']
  })
  export class MyComponent implements OnInit {
  
    constructor(private elementRef: ElementRef) {
      //stuffs
    }
  
    ngAfterViewInit() {
      // things
    }
  
    ngOnInit() {
    }
  }
  </code></pre>
  
  <p>and the test:</p>
  
  <pre class="lang-ts prettyprint-override"><code>import {
    beforeEach,
    beforeEachProviders,
    describe,
    expect,
    it,
    inject,
  } from '@angular/core/testing';
  import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
  import { Component, Renderer, ElementRef } from '@angular/core';
  import { By } from '@angular/platform-browser';
  
  describe('Component: My', () =&gt; {
    let builder: TestComponentBuilder;
  
    beforeEachProviders(() =&gt; [MyComponent]);
    beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder) {
      builder = tcb;
    }));
  
    it('should inject the component', inject([MyComponent],
        (component: MyComponent) =&gt; {
      expect(component).toBeTruthy();
    }));
  
    it('should create the component', inject([], () =&gt; {
      return builder.createAsync(MyComponentTestController)
        .then((fixture: ComponentFixture&lt;any&gt;) =&gt; {
          let query = fixture.debugElement.query(By.directive(MyComponent));
          expect(query).toBeTruthy();
          expect(query.componentInstance).toBeTruthy();
        });
    }));
  });
  
  @Component({
    selector: 'test',
    template: `
      &lt;cp&gt;&lt;/cp&gt;
    `,
    directives: [MyComponent]
  })
  class MyTestController {
  }
  </code></pre>
  
  <p>Both the component and the test blueprint have been generated by Angular-cli. Now, I can't figure out which provider, if any, I should add in the <code>beforeEachProviders</code> for the injection of ElementRef to be successful. When I run <code>ng test</code> I got <code>Error: No provider for ElementRef! (MyComponent -&gt; ElementRef)</code>.</p>