Инъекционная служба в тесте угловых установок - PullRequest
0 голосов
/ 24 октября 2018

Сначала я пытаюсь исправить базовый модульный тест, который написал для меня 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

1 Ответ

0 голосов
/ 07 марта 2019

Я потратил пару часов на решение своих проблем.Здесь ответ.Например, нам нужно ввести 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();
    });
  });
});
...