Как предоставить введенные значения при модульном тестировании компонента в angular5 + - PullRequest
0 голосов
/ 17 января 2019

Я пишу тесты для компонента, который динамически инициализируется как модальный (entryComponent). Входы для этого компонента извлекаются через инжектор. Мне нужен способ предоставить эти входные данные на этапе создания компонента в beforeEach.

this.modalService.create({
    component: sampleComponent, inputs: {
        test: 'testMsg'
    }
});

SampleComponent:

@Modal()
@Component({
    selector: 'sample-component',
    templateUrl: './sample.component.html',
    styleUrls: ['./sample.component.scss']
})
export class SampleComponent implements OnInit {
    test: string;

    constructor(private injector: Injector) {
    }

    ngOnInit() {
        this.test= this.injector.get('test');
    }
}

Тест для образцаКомпонент:

describe('sampleComponent', () => {
    let component: SampleComponent;
    let fixture: ComponentFixture<SampleComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [SampleComponent],
            imports: [
                ModalModule,
                BrowserAnimationsModule,
            ],
            providers: [
                ModalService,
            ]
        })
        .compileComponents();
        })
    );
    beforeEach(() => {
        fixture = TestBed.createComponent(SampleComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
        component.ngOnInit();
    });
    it('should create', () => {
        expect(component).toBeTruthy();
    });
});

тест не пройден с: Ошибка: StaticInjectorError (DynamicTestModule) [тест]: StaticInjectorError (Платформа: ядро) [тест]: NullInjectorError: Нет провайдера для теста!

Как обеспечить значение для «теста» в этом случае?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...