我有一个用 ngComponentOutlet 创建的组件
<ng-container *ngComponentOutlet="adminTableComponent; injector: adminTableInjector;"></ng-container>
和组件本身
import { Component, OnInit, Injectable, EventEmitter } from '@angular/core';
import { Data } from '@angular/router';
@Injectable()
export class AdminTableInfo {
resourceData;
resourceConfiguration;
resourceName;
constructor(resourceData, resourceConfiguration, resourceName) {
this.resourceData = resourceData;
this.resourceConfiguration = resourceConfiguration;
this.resourceName = resourceName;
}
}
@Component({
selector: 'rw-admin-table',
templateUrl: './admin-table.component.html',
styleUrls: ['./admin-table.component.scss']
})
export class AdminTableComponent implements OnInit {
private _resourceData;
constructor(public adminTableInfo: AdminTableInfo) {
if (adminTableInfo) {
this._resourceData = adminTableInfo.resourceData;
}
}
ngOnInit() {
this.setTableStyle();
this.setDefaultSort();
this.adminTableInfo.resourceDataChanged$.subscribe(data => {
this._resourceData = data;
this.setDefaultSort();
});
}
...
如何设置单元测试?我们正在使用 Jest,但我认为设置应该与 Jasmine/Karma 非常相似
这是我到目前为止的单元测试
@Injectable()
export class AdminTableInfo {
resourceData;
resourceConfiguration;
resourceName;
constructor(resourceData, resourceConfiguration, resourceName) {
this.resourceData = resourceData;
this.resourceConfiguration = resourceConfiguration;
this.resourceName = resourceName;
}
}
describe('AdminTableComponent', () => {
let component: AdminTableComponent;
let fixture: ComponentFixture<AdminTableComponent>;
let fakeAdminTableInfo: AdminTableInfo;
beforeEach(async(() => {
MockConfiguration
.getAdminTestBedConfiguration()
.configureTestingModule({
declarations: [
AdminTableComponent,
AdminTableRowComponent,
],
providers: [
AdminTableInfo
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AdminTableComponent);
component = fixture.componentInstance;
// Not sure i need to create an instance like this of the injectable
fakeAdminTableInfo = fixture.debugElement.injector.get(AdminTableInfo);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我遇到了错误:
Can't resolve all parameters for AdminTableInfo:
(?, ?, ?).
这意味着我在创建组件时没有注入 AdminTableInfo。有谁知道如何为这种情况设置单元测试?