Angular 4 - testing the router.url module

How do I execute mock router.url in angular 4 unit testing?

I use router.url in ngOnint in my component, but in my test the value for router.url is '/'

+4
source share
1 answer

you can use jasmine spyObj. In you TestBed:

providers:[
 {
 provide: Router,
 usevalue: jasmine.createSpyObj('Router',['methodOne','methodTwo]),
 },
],

in beforeEach:

router = fixture.debugElement.injector.get(Router);

in the test:

it('should...', ()=> {
 (router.methodOne as Spy).and.returnValue(whateverValue)// if you wanna mock the response
});
+2
source

Source: https://habr.com/ru/post/1685844/


All Articles