Angular 为“应用”应用值;公共数据:任何“;单元测试中

Angular 为“应用”应用值;公共数据:任何“;单元测试中,angular,visual-studio-code,Angular,Visual Studio Code,目标: 将名为“Same message”的单元测试设置为成功或绿色 问题: 我尝试使用不同的语法和解决方案,为了实现目标,我缺少了什么 信息: 谢谢大家! alert-dialog.component.spec.ts import { async, ComponentFixture, TestBed } from "@angular/core/testing"; import { AlertDialogComponent } from "./alert-di

目标:
将名为“Same message”的单元测试设置为成功或绿色

问题:
我尝试使用不同的语法和解决方案,为了实现目标,我缺少了什么

信息:

谢谢大家!


alert-dialog.component.spec.ts

import { async, ComponentFixture, TestBed } from "@angular/core/testing";

import { AlertDialogComponent } from "./alert-dialog.component";
import { MatButtonModule } from "@angular/material/button";
import {
  MatDialogModule,
  MAT_DIALOG_DATA,
  MatDialogRef,
} from "@angular/material/dialog";

describe("AlertDialogComponent", () => {
  let component: AlertDialogComponent;
  let fixture: ComponentFixture<AlertDialogComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [AlertDialogComponent],
      imports: [MatDialogModule],
      providers: [
        {
          provide: MAT_DIALOG_DATA,
          useValue: {},
        },
        {
          provide: MatDialogRef,
          useValue: {},
        },
      ],
    }).compileComponents();
  }));

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

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

  it("Same message", () => {
    expect(component.message).toEqual("test");
  });
});
import{async,ComponentFixture,TestBed}来自“@angular/core/testing”;
从“/alert dialog.component”导入{AlertDialogComponent}”;
从“@angular/material/button”导入{MatButtonModule}”;
进口{
MatDialogModule,
材料对话框数据,
MatDialogRef,
}从“@角度/材质/对话框”;
描述(“AlertDialogComponent”,()=>{
let组件:AlertDialogComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[AlertDialogComponent],
导入:[MatDialogModule],
供应商:[
{
提供:MAT_DIALOG_数据,
useValue:{},
},
{
提供:MatDialogRef,
useValue:{},
},
],
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(AlertDialogComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它(“应该创建”,()=>{
expect(component.toBeTruthy();
});
它(“相同的消息”,()=>{
expect(component.message).toEqual(“测试”);
});
});

alert-dialog.component.html

<mat-dialog-content>
  {{ message }}
</mat-dialog-content>
<mat-dialog-actions align="end">
  <button mat-raised-button color="primary" mat-dialog-close tabindex="-1">
    {{ cancelButtonText }}
  </button>
</mat-dialog-actions>

{{message}}
{{cancelButtonText}

alert-dialog.component.ts

import { Component, OnInit, Inject } from "@angular/core";
import {
  MAT_DIALOG_DATA,
  MatDialogRef,
  MatDialog,
} from "@angular/material/dialog";
import { MatSnackBar } from "@angular/material/snack-bar";

@Component({
  selector: "app-alert-dialog",
  templateUrl: "./alert-dialog.component.html",
  styleUrls: ["./alert-dialog.component.css"],
})
export class AlertDialogComponent implements OnInit {
  message: string = "";
  cancelButtonText = "Cancel";
  constructor(
    public dialogRef: MatDialogRef<AlertDialogComponent>,
    @Inject(MAT_DIALOG_DATA)
    public data: any
  ) {
    if (data) {
      this.message = data.message || this.message;

      if (data.buttonText) {
        this.cancelButtonText = data.buttonText.cancel || this.cancelButtonText;
      }
    }

    //this.dialogRef.updateSize("300vw", "300vw");
  }

  ngOnInit(): void {}

  onConfirmClick(): void {
    this.dialogRef.close(true);
  }
}
import{Component,OnInit,Inject}来自“@angular/core”;
进口{
材料对话框数据,
MatDialogRef,
MatDialog,
}从“@角度/材质/对话框”;
从“@angular/material/snackbar”导入{matsnakbar}”;
@组成部分({
选择器:“应用程序警报对话框”,
templateUrl:“./alert dialog.component.html”,
样式URL:[“/alert dialog.component.css”],
})
导出类AlertDialogComponent实现OnInit{
消息:string=“”;
cancelButtonText=“取消”;
建造师(
公共dialogRef:MatDialogRef,
@注入(材料对话框数据)
公共数据:任何
) {
如果(数据){
this.message=data.message | | this.message;
if(data.buttonText){
this.cancelbuttonext=data.buttonext.cancel | | this.cancelbuttonext;
}
}
//此.dialogRef.updateSize(“300vw”、“300vw”);
}
ngOnInit():void{}
onConfirmClick():void{
此.dialogRef.close(true);
}
}