spectator: "NullInjectorError: No provider for Location!" when using createRoutingFactory

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[x] Support request
[ ] Other... Please describe:

Current behavior

When trying to use createRoutingFactory, I get the issue seen here.

Expected behavior

createRoutingFactory should automatically handle mocking the Router, but it appears that it isn’t. I feel like I’m probably doing something wrong here.

Minimal reproduction of the problem with instructions

For bug reports please provide the STEPS TO REPRODUCE and if possible a MINIMAL DEMO of the problem via https://stackblitz.com or similar (you can use this template as a starting point: https://bit.ly/2zme3bj).

Constructor for component under test (TimeEntryPage):

    constructor(
        private controlNumberService: ControlNumbersService,
        private fb: FormBuilder,
        private modalController: ModalController,
        private route: ActivatedRoute,
        private router: Router,
        private timeout: TimeoutService
    )

where ControlNumberService and TimeoutService are custom services, ModalController is an Ionic service, and FormBuilder, ActivatedRoute, and Router are Angular services.

Component test:

describe(`TimeEntryPage`, () => {
    const controlNumbersService: StubControlNumbersService = new StubControlNumbersService();
    let router: SpyObject<Router>;
    let spectator: Spectator<TimeEntryPage>;

    const createComponent = createRoutingFactory({
        component: TimeEntryPage,
        params: { controlNumber: '23836110' },
        mocks: [ModalController, TimeoutService],
        providers: [
            {
                provide: ControlNumbersService,
                useValue: controlNumbersService
            },
            FormBuilder
        ],
        imports: [IonicModule, ReactiveFormsModule, SharedModule],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    });

    afterEach(() => {
        spectator.debugElement.nativeElement.remove();
    });

    beforeEach(() => {
        route.testParams = { controlNumber: '23836110' };

        spectator = createComponent({ detectChanges: false });

        router = spectator.get(Router);
    });

    it(`should instantiate`, () => {
        expect(spectator.component).toBeDefined();
    });
});

What is the motivation / use case for changing the behavior?

N/A

Environment


Angular version: 8.3.2 (latest)


Browser:
- [x] Chrome (desktop) version 76.0.3809.132
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v11.15.0  
- Platform: macOS 10.14.6 

Others:


- Jasmine + Karma (built-in with Angular CLI project)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@NetanelBasal - very much so! Thank you and @dirkluijk for your help! You guys really are amazing examples when it comes open source project management. Keep up the good work!

I checked the PR in the example repo, and with stubsEnabled: false it seems to work now. Let’s merge and release it.

I will submit a PR sometime tomorrow.

@NetanelBasal Sure thing. This should do it.

N.B. it is an Ionic project, but that shouldn’t matter for running the tests since it still uses Karma + Jasmine

Currently on vacation but yeah, I will try to investigate asap!