Angular unit test: Can't resolve all parameters for component

1.9k views Asked by At

OK, so I've been trying to figure this out for a while now, and all I can find online (regarding the error message in the title) refers to circular dependencies during DI - but I'm 99% certain that's not my issue.

So, here goes - I get the following error when running my test:

Error: Can't resolve all parameters for AssetGalleryCardComponent: ([object Object], [object Object], ?)

Component CTOR:

constructor(@Inject('assetService') private assetService: AssetService,
            @Inject('assetValidation') private assetValidation: AssetValidationService,
            @Inject(AssetGalleryService) private assetGalleryService: AssetGalleryService) { }

Test-code:

import { AssetGalleryService } from './../asset-gallery.service';
import { AssetCardCaptionDirective } from './../../asset-card-caption.directive';
import { MaterialModule } from '@angular/material';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AssetGalleryCardComponent } from './asset-gallery-card.component';

describe('asset-gallery-card.component', () => {
    let component: AssetGalleryCardComponent;
    let fixture: ComponentFixture<AssetGalleryCardComponent>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [
                MaterialModule
            ],
            declarations: [AssetCardCaptionDirective, AssetGalleryCardComponent],
            providers: [
                {
                    provide: AssetGalleryService,
                    useValue: {}
                },
                {
                    provide: 'assetValidation',
                    useValue: {}
                },
                {
                    provide: 'assetService',
                    useValue: {}
                }
            ]
        });

        fixture = TestBed.createComponent(AssetGalleryCardComponent);
        component = fixture.componentInstance;
    });

    it('should be defined', () => {
        expect(component).toBeDefined();
    });
});

I've tried stripping the test-module setup and adding stuff as the test runner asks for it, and only when I add the mock for AssetGalleryService as the last bit, errors start throwing.

As you see, I've mocked all dependencies, since the current state of the test don't require anything from the services.

There are no barrel imports (I've read that might lead to this issue as well)

Only thing being imported "twice" is AssetGalleryService which is imported in both the test-file and in the component file. If I switch the order of imports in the test file, I get this error:

Error: No provider for $injector!

Which is probably related to the fact that I'm running a hybrid app? 2 of the services for the component are AngularJS services.

Edit: If I add forwardRef(() => ... ) to the @Inject(AssetGalleryService)... in the component CTOR, I get the same error as above when running tests.

Any hints are very welcome!

0

There are 0 answers