Problems with ActivatedRoute in Standalone Component #3634
-
I am having an issue with using The Component is defined like this: @Component({
standalone: true,
imports: [CommonModule, RouterModule, MatListModule],
templateUrl: './my.component.html',
styleUrls: ['./my.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComponent {} The template is: <mat-nav-list>
<a mat-list-item [routerLink]="['link']">Link</a>
</mat-nav-list> My test is still at the it('should create', () => {
const fixture = TestBed.createComponent(MyComponent);
const component = fixture.componentInstance;
expect(component).toBeTruthy();
}); I have recently switched to using MockBuilder(MyComponent); but this resulted in:
Looking through the documentation suggested that: MockBuilder(MyComponent)
.keep(RouterModule)
.keep(RouterTestingModule) might work, but I get the same result. I also tried: MockBuilder(MyComponent)
.mock(RouterModule)
.keep(RouterTestingModule) and MockBuilder(MyComponent)
.exclude(RouterModule)
.keep(RouterTestingModule) with no luck. I even tried: MockBuilder(MyComponent)
.provide(MockProvider(ActivatedRoute)); but it still tells me I have no provider for TestBed.configureTestingModule({
imports: [MyComponent, RouterTestingModule]
}).compileComponents(); the test passes. It's probably something really stupid, but I just can't see it. Any help would be gratefully received. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi there,
Your last try with |
Beta Was this translation helpful? Give feedback.
Hi there,
ActivatedRoute
isn't a part ofRouterModule
, it's defined in.forRoot
and in.forChild
.Because your component imports
RouterModule
only, you need to provideActivatedRoute
additionally forTestBed
.Your last try with
.provide
is the right one and works correctly: https://codesandbox.io/s/sweet-rubin-mx7owq?file=/src/test.spec.ts