1

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!

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26
  • "circular dependencies" is only one of several causes for this error. `forwardRef()` is only necessary if an identifier is referenced in code that is only declared further down in the same file (at least I haven't seen another situation where `forwardRef` would have been required). – Günter Zöchbauer Sep 04 '17 at 11:38
  • What does the constructor for `AssetGalleryService` look like? I'm not sure if Angular analyzes the constructor parameters if `useValue` is used, but it's possible. Do you get a different error message if you use `provide: AssetGalleryService, useClass: AssetGalleryService`? – Günter Zöchbauer Sep 04 '17 at 11:39
  • there's not constructor - it has no external dependencies.. Same error if I do `{useClas ...}` (which, AFAIK, is the same as not using `{provide ...}` syntax?) – Thor Jacobsen Sep 04 '17 at 11:40
  • I see. Then I don't know. I don't have much knowledge about testing in TS. – Günter Zöchbauer Sep 04 '17 at 11:41
  • @Inject( forwardRef( () => AssetGalleryService ) assetGalleryService – Milad Sep 04 '17 at 11:45
  • @Milad as I wrote near the bottom of the question, that just introduces another error (`No provider for $Injector`) – Thor Jacobsen Sep 04 '17 at 11:49
  • How come AssetGalleryService is a service, which I assume is an Injectable class, but you're using `useValue` when overriding it ? You can't do that, it should be useClass and you should provide a MockAssetGalleryService – Milad Sep 04 '17 at 12:03
  • I've tried that as well, same result. AFAIK, it does not matter what you use. These should all do the same: `{useClass: MyClass}`, `{useValue: new MyClass()}` and `{useFactory: () => new MyClass()}` – Thor Jacobsen Sep 04 '17 at 12:14

0 Answers0