【问题标题】:Issue testing a directive in angular问题以角度测试指令
【发布时间】:2019-12-24 05:18:15
【问题描述】:

这是我的指令规范文件。我正在使用角度 6。

import { Component, DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { KeyboardInputDirective } from './keyboard-input.directive';

@Component({
  selector: 'kiosk-app-test-component',
  template: `<input type="text" name="wu-input" class="wu-input" id="standard" [kioskAppKeyboardInput]="'true'">`
})
class TestStubComponent { }


describe('Directive: KeyboardInputDirective', () => {
  let component: TestStubComponent;
  let fixture: ComponentFixture<TestStubComponent>;
  let directiveEl: DebugElement;

  beforeEach(async(() => {

    TestBed.configureTestingModule({
      declarations: [TestStubComponent, KeyboardInputDirective],
      schemas: [NO_ERRORS_SCHEMA]
    });

    TestBed.compileComponents().then(() => {
      fixture = TestBed.createComponent(TestStubComponent);
      component = fixture.componentInstance;
      directiveEl = fixture.debugElement.query(By.directive(KeyboardInputDirective));
    });

  }));

  fit('should create an instance', () => {
    fixture.detectChanges();
    expect(directiveEl).not.toBeNull();
  });
});

我不断低于错误。

TypeError: Cannot read property 'query' of null

如果我控制台 fixture.debugElement 值它始终为空。 非常感谢任何帮助。

package.json

{
  "name": "project-name-xyz",
  "private": true,
  "scripts": {
    "ng": "$(npm bin)/ng",
    "start:web": "$(npm bin)/ng serve web",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  },
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "@angular/upgrade": "^6.0.4",
    "@ngrx/effects": "^7.2.0",
    "@ngrx/entity": "^7.2.0",
    "@ngrx/store": "^6.1.0",
    "@ngrx/store-devtools": "^7.2.0",
    "@ngx-translate/core": "^10.0.2",
    "@types/angular": "^1.6.45",
    "angular": "^1.6.9",
    "angular4-translate": "^1.3.5",
    "async": "^2.6.1",
    "child-process-promise": "^2.2.1",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.5.4",
    "fetch-intercept": "^2.3.0",
    "fingerprintjs2": "1.1.0",
    "fs-extra": "^7.0.0",
    "google-libphonenumber": "3.2.6",
    "gulp": "^3.9.1",
    "gulp-remove-code": "^3.0.4",
    "gulp-rename": "^1.4.0",
    "gulp-replace": "^1.0.0",
    "husky": "^3.0.5",
    "jquery": "^3.3.1",
    "lodash": "^4.17.10",
    "moment": "^2.22.2",
    "ngrx-store-localstorage": "^5.1.0",
    "ngx-bootstrap": "^3.3.0",
    "ngx-cookie-service": "^1.0.10",
    "ngx-logger": "^3.3.12",
    "ngx-mask": "^6.1.3",
    "ngx-page-scroll-core": "^6.0.2",
    "ngx-popper": "^5.1.7",
    "popper.js": "^1.14.4",
    "protractor-axe-report-plugin": "^1.1.0",
    "run-sequence": "^2.2.1",
    "rxjs": "6.0.0",
    "rxjs-compat": "^6.2.2",
    "save": "^2.3.2",
    "systemjs": "^0.21.4",
    "systemjs-plugin-babel": "0.0.25",
    "virtual-keyboard": "^1.28.4",
    "web-animations-js": "^2.3.1",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.13.1",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^7.2.10",
    "@angular/language-service": "^6.0.3",
    "@ngrx/schematics": "^7.2.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/lodash": "^4.14.118",
    "@types/node": "~8.9.4",
    "chalk": "^2.4.2",
    "codelyzer": "~4.2.1",
    "fancy-log": "^1.3.3",
    "gulp-clean": "^0.4.0",
    "gulp-i18n-lint": "^0.1.1",
    "gulp-tap": "^1.0.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-typescript": "^4.0.0",
    "protractor": "^5.4.0",
    "sonarqube-scanner": "^2.4.1",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~3.1.1"
  }
}

【问题讨论】:

    标签: javascript angular jasmine


    【解决方案1】:

    您可能需要将 fixture.detectChanges(); 移动到您的 describe 中,如下所示。

    describe('Directive: KeyboardInputDirective', () => {
      let component: TestStubComponent;
      let fixture: ComponentFixture<TestStubComponent>;
      let directiveEl: DebugElement;
    
      beforeEach(async(() => {
    
        TestBed.configureTestingModule({
          declarations: [TestStubComponent, KeyboardInputDirective],
          schemas: [NO_ERRORS_SCHEMA]
        });
    
        TestBed.compileComponents().then(() => {
          fixture = TestBed.createComponent(TestStubComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();
          directiveEl = fixture.debugElement.query(By.directive(KeyboardInputDirective));
        });
    
      }));
    

    希望这会有所帮助,如果您遇到问题,请告诉我。

    【讨论】:

    • 感谢您的帮助,但尝试了同样的错误。
    • 你能添加你的 pacakge.json 内容吗,你的代码对我来说很好用
    • 编辑了发布的原始问题。
    • 它对我来说很好用,你可以尝试在 stackblitz 上复制它吗?你可以参考这个演示 - stackblitz.com/edit/angular-unit-testing-example
    【解决方案2】:

    在这种情况下不需要TestBed.compileComponents,但需要在查询HTML 元素之前调用fixture.detectChanges。您可以直接寻址(查询)input 元素并在您的测试中与它交互以查看指令是否正常工作。不知道您的指令的真正作用,显然需要对下面的测试进行调整以使其适合您。

    describe('Directive: KeyboardInputDirective', () => {
    
      let component: TestStubComponent;
      let fixture: ComponentFixture<TestStubComponent>;
      let inputElement: HTMLInputElement;
    
      beforeEach(() => {
        TestBed.configureTestingModule({
          declarations: [TestStubComponent, KeyboardInputDirective]
        });
        fixture = TestBed.createComponent(TestComponent);
        fixture.detectChanges();
        component = fixture.componentInstance;
        inputElement = <HTMLInputElement>fixture.debugElement.nativeElement.querySelector('INPUT');
      });
    
      ...
    
      it('#keydown should ...', fakeAsync(() => {
    
        // given
        spyOn(inputElement, 'setSelectionRange');
        inputElement.value = 'abc';
        const event = new KeyboardEvent('keydown', { key: '' });
    
        // when
        inputElement.dispatchEvent(event);
        tick();
    
        // then
        expect(inputElement.setSelectionRange).toHaveBeenCalledWith(0, 3);
      }));
    

    【讨论】:

    • 感谢您的帮助,但出现此错误:TypeError: Cannot read property 'nativeElement' of null
    猜你喜欢
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多