Angular 4 - Failed: Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)

30,445

Solution 1

You want to inject a fake ActivatedRoute to your component, since you create it yourself in the test, and the router thus doesn't create it for you and inject an ActivatedRoute. So you can use something like this:

describe('SomeComponent', () => {

  const fakeActivatedRoute = {
    snapshot: { data: { ... } }
  } as ActivatedRoute;

  beforeEach(async(() => {
    TestBed.configureTestingModule({ 
      imports: [ RouterTestingModule ],
      declarations: [ SomeComponent ],
      providers: [ {provide: ActivatedRoute, useValue: fakeActivatedRoute} ],
    })
    .compileComponents();
  }));
});

Solution 2

Here's a solution for angular 7

{
    provide: ActivatedRoute,
    useValue: {
        snapshot: {
            paramMap: {
                get(): string {
                    return '123';
                },
            },
        },
    },
},

Solution 3

{
  provide: ActivatedRoute,
  useValue: {
    snapshot: {
      queryParamMap: {
        get(): number {
          return 6;
        }
      }
    }
  }
}
Share:
30,445
Lakindu Gunasekara
Author by

Lakindu Gunasekara

Senior Software Engineer at 99x AWS Certified Solutions Architect - Associate | GSoCer | GCI mentor LinkedIn Profile Top Bootstrap contributors in Sri Lanka

Updated on July 18, 2020

Comments

  • Lakindu Gunasekara
    Lakindu Gunasekara almost 4 years

    I have referred the following link to get the answers, but I couldn't find any working solution for my scenario. Error: (SystemJS) Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)

    Therefore, I have been trying to remove the Activated Route from the providers and still the test bed is not passing. It shows

    Error: No provider for ActivatedRoute!

    So here is my code, I want to run my test bed in the angular application which is using Jasmine.

    import { ActivatedRoute } from '@angular/router';
    import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    import { RouterModule, Routes } from '@angular/router';
    import { RouterTestingModule } from '@angular/router/testing';
    
    describe('SomeComponent', () => {
      let component: SomeComponent;
      let fixture: ComponentFixture<SomeComponent>;
    
      beforeEach(async(() => {
        TestBed.configureTestingModule({ 
          imports: [ RouterModule, RouterTestingModule ],
          declarations: [ SomeComponent ],
          providers: [ ActivatedRoute ],
        })
        .compileComponents();
      }));
    
      beforeEach(() => {
        fixture = TestBed.createComponent(SomeComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    Error getting

    enter image description here

  • Zerok
    Zerok over 5 years
    Thing is, how do you populate the snapshot object so it works?
  • Lauri Elias
    Lauri Elias over 5 years
    Why do I HAVE to fake it? Is there any example on the internet where I actually simulate reading URLs?