【发布时间】:2018-06-12 12:56:09
【问题描述】:
我有一个通过@Attribute 注入属性的组件:
@Component({
selector: 'app-foo',
templateUrl: './foo.component.html'
})
export class FooComponent implements OnInit {
constructor(@Attribute('foo') private foo: string) {
// do something with foo
}
}
现在我想用 Jasmine 和 Karma 编写一个测试。不幸的是,我找不到任何关于如何通过TestBed 注入器在测试中提供此属性的文档。
这是我尝试过的:
describe('FooComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{provide: 'foo', useValue: 'bar'},
{provide: Attribute('foo'), useValue: 'bar'},
],
declarations: [FooComponent],
})
.compileComponents();
}));
it('should merge outer class', () => {
const fixture = TestBed.createComponent(FooComponent);
const component = fixture.componentInstance;
fixture.detectChanges();
// do actual testing
});
});
经过一些研究,我还定义了以下但没有成功:
Inject('foo')(FooComponent, null, 0);
Attribute('foo')(FooComponent, null, 0);
传递给构造函数的参数总是null。有人知道解决方案吗?我正在使用 Angular 5.2.10。
【问题讨论】:
标签: angular testing dependency-injection