带有模拟结构指令的 Angular 测试组件失败

Fel*_*lix 2 angular-directive angular angular-test

我正在尝试在组件测试中模拟结构指令,但出现错误。

以下测试失败并显示一条消息:

嵌入模板上的任何指令都未使用属性绑定 appSome。确保属性名称拼写正确,并且所有指令都列在“@NgModule.declarations”中。("[错误 ->] 测试

我嘲笑结构指令SomeDirectiveSomeMockDirectiveapp.component.spec.ts中定义。测试失败

如果我切换声明使其包含SomeDirective-测试通过

我想知道为什么我不能让它与模拟版本一起工作。

模板:

<h1 *appSome="true">TEST</h1>
Run Code Online (Sandbox Code Playgroud)

指令(生产种类:)):

@Directive({
  selector: '[appSome]'
})
export class SomeDirective implements OnDestroy {
  show: boolean;
  ...
}


Run Code Online (Sandbox Code Playgroud)

测试:

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { Directive, NO_ERRORS_SCHEMA } from '@angular/core';

@Directive({
  selector: '[appSome]'
})
export class SomeMockDirective {}

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [AppComponent, SomeMockDirective], // test is failing, switch the directive to SomeDirective and it passes
      schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

复制仓库:https : //github.com/felikf/angular-test-directive

git clone https://github.com/felikf/angular-test-directive.git 
npm i
ng test
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么模拟版本不起作用,即使提供了模拟版本providers: []并且指令具有相同的选择器。

yur*_*zui 7

模拟版本不起作用,因为您尚未定义appSome输入属性:

@Directive({
  selector: '[appSome]'
})
export class SomeMockDirective {
  @Input() appSome;  <================= add this
}
Run Code Online (Sandbox Code Playgroud)