Я пытаюсь сначала исправить базовый модульный тест, который для меня написал cli. Я новичок в модульном тестировании, поэтому вот код
import { MyDirective } from './my.directive';
describe('MyDirective', () => {
it('should create an instance', () => {
const directive = new MyDirective(); // it throws error here
expect(directive).toBeTruthy();
});
});
Мне нужно ввести ElementRef внутри MyDirective
Я потратил пару часов на решение проблемы. Вот ответ. Например, нам нужно ввести ElementRef и NgControl:
import { inject } from '@angular/core/testing';
import { ElementRef } from '@angular/core';
import { NgControl } from '@angular/forms'
import { MyDirective } from './my.directive';
describe('MyDirective', () => {
it('should create an instance', inject([ElementRef, NgControl], (elementRef: ElementRef, ngControl: NgControl) => {
const directive = new MyDirective(elementRef, ngControl);
expect(directive).toBeTruthy();
}));
});
});
Может быть, вам стоит начать с чтения официальная документация, который отвечает на этот случай.