Angular 如何使用属性模拟MAT_对话框_数据

Angular 如何使用属性模拟MAT_对话框_数据,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我需要测试一个有MAT_DIALOG_数据的组件,我甚至可以模拟它,但我在数据上收到的属性有问题 我相信模拟是正确的,但我不知道如何将属性放在变量数据上 *通知编辑.component.spec.ts fdescribe('NotificationsEditComponent', () => { let component: NotificationsEditComponent; let fixture: ComponentFixture<NotificationsEditC

我需要测试一个有MAT_DIALOG_数据的组件,我甚至可以模拟它,但我在数据上收到的属性有问题

我相信模拟是正确的,但我不知道如何将属性放在变量数据上

*通知编辑.component.spec.ts

fdescribe('NotificationsEditComponent', () => {
  let component: NotificationsEditComponent;
  let fixture: ComponentFixture<NotificationsEditComponent>;
  let dialogMock: jasmine.SpyObj<any>;
  let dialogRefMock: jasmine.SpyObj<any>;
  
  beforeEach(waitForAsync(() => {

    dialogRefMock = jasmine.createSpyObj('MatDialogRef', [
      'close',
    ]);

    TestBed.configureTestingModule({
      imports: [
        CommonModule,
        ReactiveFormsModule,
        FormsModule,
        AppModule,
        MatDialogModule,
        MatFormFieldModule,
        MatInputModule,
      ],
      declarations: [ NotificationsEditComponent ],
      providers: [
        { provide: MatDialogRef, useValue: dialogMock },
        { provide: MAT_DIALOG_DATA, useValue: {} },
      ]
    })
    .compileComponents();
  }));
  constructor(
    public dialog: MatDialog,
    public dialogRef: MatDialogRef<NotificationsEditComponent>,
    @Inject(MAT_DIALOG_DATA) public notification: any
  ) {}

  ngOnInit() {
    this.form = new FormGroup({
      frequency: new FormControl(),
      topic: new FormControl(),
      title: new FormControl(),
      time: new FormControl(),
      day: new FormControl(),
      body: new FormControl()
    })
    this.form.setValue({
      frequency: this.notification.frequency,
      topic: this.notification.topic,
      title: this.notification.payload.notification.title,
      time: this.notification.time.seconds,
      day: this.notification.day.day,
      body: this.notification.payload.notification.body
    })
  }

您是否已经厌倦了为MAT_DIALOG_数据创建模拟? 正如我所看到的,你需要一些属性,所以你应该尝试一下

const notificationMock={
频率:你的价值观,
主题:你的价值观,
有效载荷:{
通知:{
标题:yourmockvalue,
body:yourmockvalue
}
},
时间:{
秒:yourmockvalue
},
日期:{
戴:你的价值观
},
}
TypeError: Cannot read property 'notification' of undefined
    at NotificationsEditComponent.ngOnInit (http://localhost:9876/_karma_webpack_/webpack:/src/app/modules/notifications/pages/notifications-edit/notifications-edit.component.ts:108:23)
    at callHook (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:2521:1)
    at callHooks (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:2492:1)
    at executeInitAndCheckHooks (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:2443:1)
    at refreshView (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:9429:1)
    at renderComponentOrTemplate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:9528:1)
    at tickRootContext (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:10754:1)
    at detectChangesInRootView (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:10779:1)
    at RootViewRef.detectChanges (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:22792:1)
    at ComponentFixture._tick (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/testing.js:141:1)