gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:01:21 24 4
gpt4 key购买 nike

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

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

Property binding appSome not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("[ERROR ->]TEST



我在 mock 结构指令 SomeDirectiveSomeMockDirective在 app.component.spec.ts 中定义。 测试失败 .

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

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

模板:

<h1 *appSome="true">TEST</h1>

指令(生产种类:)):

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



测试:

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();
});
});

复制 repo :
https://github.com/felikf/angular-test-directive
git clone https://github.com/felikf/angular-test-directive.git 
npm i
ng test

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

最佳答案

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

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

关于带有模拟结构指令的 Angular 测试组件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58024047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com