我有一个登录组件,如下所示:

import { Component, OnInit } from '@angular/core';
import { MdDialogRef } from '@angular/material';

import { AuthService } from '../../core/services/auth.service';

@Component({
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginDialogComponent implements OnInit {

  model: {
    email: string,
    password: string
  };
  error;

  constructor(
    private authService: AuthService,
    private dialogRef: MdDialogRef<LoginDialogComponent>
  ) { }

  ngOnInit() {
    this.model = {
      email: '',
      password: ''
    };
  }

  signin() {
    this.error = null;
    this.authService.login(this.model.email, this.model.password).subscribe(data => {
      this.dialogRef.close(data);
    }, err => {
      this.error = err.json();
    });
  }

}

我有一个这个组件的测试规范如下:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MdDialogRef, OverlayRef  } from '@angular/material';

import { AuthService } from '../../core/services/auth.service';
import { LoginDialogComponent } from './login.component';

describe('Component: Login', () => {

    let component: LoginDialogComponent;
    let fixture: ComponentFixture<LoginDialogComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                LoginDialogComponent
            ],
            imports: [],
            providers: [
                AuthService,
                MdDialogRef,
                OverlayRef
            ]
        })
            .compileComponents();
    }));

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

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

我尝试了无数种不同的方法,不管我做什么,我都会得到以下错误:
无法解析mdDialogRef:(?)的所有参数
这是mddialogref的代码,它只有一个参数overlayref。我错过了什么?
import { OverlayRef } from '../core';
import { Observable } from 'rxjs/Observable';
/**
 * Reference to a dialog opened via the MdDialog service.
 */
export declare class MdDialogRef<T> {
    private _overlayRef;
    /** The instance of component opened into the dialog. */
    componentInstance: T;
    /** Subject for notifying the user that the dialog has finished closing. */
    private _afterClosed;
    constructor(_overlayRef: OverlayRef);
    /**
     * Close the dialog.
     * @param dialogResult Optional result to return to the dialog opener.
     */
    close(dialogResult?: any): void;
    /** Gets an observable that is notified when the dialog is finished closing. */
    afterClosed(): Observable<any>;
}

编辑:根据@ryan的评论,我尝试完全删除mddialogref提供程序,得到以下错误:
无法解析overlayref:(???)
这使我相信问题实际上是w/mddialogref试图解决overlayref,而不是w/mddialogref本身。
工作示例根据yurzui的建议,下面的代码是实际的工作代码。
    /* tslint:disable:no-unused-variable */

import { NgModule } from '@angular/core';
import { async, TestBed } from '@angular/core/testing';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MaterialModule, MdDialogModule, MdToolbarModule, MdDialog, MdDialogRef } from '@angular/material';

import { CoreModule } from '../../core/core.module';
import { LoginDialogComponent } from './login.component';

@NgModule({
    declarations: [
        LoginDialogComponent
    ],
    entryComponents: [
        LoginDialogComponent
    ],
    exports: [
        LoginDialogComponent
    ],
    imports: [
        CommonModule,
        CoreModule,
        FormsModule,
        MaterialModule.forRoot(),
        MdDialogModule.forRoot(),
        MdToolbarModule.forRoot()
    ]
})
class LoginDialogSpecModule { }

describe('Component: Login Dialog', () => {
    let component: LoginDialogComponent;
    let dialog: MdDialog;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                LoginDialogSpecModule
            ]
        });
    });

    beforeEach(() => {
        dialog = TestBed.get(MdDialog);
        let dialogRef = dialog.open(LoginDialogComponent);
        component = dialogRef.componentInstance;
    });

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

最佳答案

有一个问题ComponentFactoryResolver is not aware of components compiled via TestBed
根据这个问题,Angular2团队提供了一种解决方法,即创建具有entryComponents属性的实际模块
https://github.com/angular/material2/blob/2.0.0-beta.1/src/lib/dialog/dialog.spec.ts#L387-L402
所以你的测试可以这样写:

import { MdDialog, MdDialogModule } from '@angular/material';

@NgModule({
    declarations: [TestComponent],
    entryComponents: [TestComponent],
    exports: [TestComponent],
})
class TestModule { }

describe('Component: Login', () => {
    let component: TestComponent;
    let dialog: MdDialog;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [TestModule, MdDialogModule]
        });
    });

    beforeEach(() => {
        dialog = TestBed.get(MdDialog);
        let dialogRef = dialog.open(TestComponent);

        component = dialogRef.componentInstance;
    });

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

Plunker Example

09-18 19:52