nik*_*ar4 3 unit-testing dependency-injection karma-jasmine angular-directive angular
我试图首先修复 cli 为我编写的基本单元测试。我是单元测试的新手,所以这是代码
import { MyDirective } from './my.directive';
describe('MyDirective', () => {
it('should create an instance', () => {
const directive = new MyDirective(); // it throws error here
expect(directive).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
我需要在 MyDirective 中注入 ElementRef
小智 5
我在解决问题的过程中浪费了几个小时。答案在这里。比如我们需要注入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();
}));
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
968 次 |
| 最近记录: |