Я довольно новичок в Angular, и я начинаю пытаться написать несколько модульных тестов.
У меня есть простой компонент (TestComponent
), который является функцией, закрывающей this.dialogRef
но я изо всех сил пытаюсь проверить, что он вызывается.
export interface SearchExtension {
value: string;
newSearch: boolean;
andSearch: boolean;
}
export class TestComponent implements OnInit {
constructor(
protected dialogRef: MatDialogRef<TestComponent>,
) {
}
testMethod(text: string) {
this.dialogRef.close(<SearchExtension>{
value: JSON.stringify(text),
newSearch: true,
andSearch: false
});
}
}
В моем классе TestComponent.spe c .ts я настроил его следующим образом:
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let dialog: jasmine.SpyObj<MatDialogRef<TestComponent>>;
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
MatMenuModule,
MatTreeModule,
MatSnackBarModule,
],
declarations: [
TestComponent,
],
providers: [
{
provide: MatDialog,
useClass: MatDialogMock
},
{ provide: MatDialogRef, useValue: {} },
{ provide: MAT_DIALOG_DATA, useValue: { properties: testResultData[0].properties } },
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.properties = testResultData[0].properties;
dialog = TestBed.get(MatDialogRef);
viewport.set('laptop');
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy('it should create');
});
it('should close the component if New is clicked', () => {
spyOn(dialog,'close');
component.newSearch('test');
expect(dialog.close(<SearchExtension>{
value: JSON.stringify('test'),
newSearch: true,
andSearch: false
})).toHaveBeenCalled();
});
});
Однако это дает мне ошибку:
close() method does not exist
Может кто-нибудь указать, где я иду не так?