title: Angular test when a function is called when activated route has params
body: <p>I am trying to write a test for a component where a function is called when activatedroute has segment <code>edit</code> in it</p>
  <pre><code>ngOnInit() {
        this.activatedRoute.url.subscribe((urlSegments) =&gt; {
          const currentRoute = urlSegments[0].path;
          console.log(currentRoute);
          if (currentRoute === &quot;edit&quot;) {
            this.isEdit = true;
            this.id = params.id;
            this.populateForm(this.id);
          }
        });
  }
  </code></pre>
  <p>this is the <code>ngOnInit</code> function and below are my tests</p>
  <pre><code>beforeEach(() =&gt; {
      mockNbToastrService = jasmine.createSpyObj([&quot;show&quot;]);
      mockNbDialogService = jasmine.createSpyObj([&quot;open&quot;]);
  
      TestBed.configureTestingModule({
        imports: [
          RouterTestingModule.withRoutes([]),
          HttpClientTestingModule,
          ReactiveFormsModule,
        ],
        declarations: [ServiceCatalogComponent],
        providers: [
          { provide: NbToastrService, useValue: mockNbToastrService },
          { provide: NbDialogService, useValue: mockNbDialogService },
          {
            provide: ActivatedRoute,
            useValue: {
              params: of({ id: 123 }),
              url: of([
                {
                  path: &quot;edit&quot;,
                },
              ]),
            },
          },
        ],
      }).compileComponents();
  
      fixture = TestBed.createComponent(ServiceCatalogComponent);
      fixture.detectChanges();
    });
  
  
  it(&quot;should be in edit mode&quot;, () =&gt; {
      expect(fixture.componentInstance.isEdit).toBeTruthy();
    })
  
    it(&quot;should have an id if in edit mode&quot;, () =&gt; {
      expect(fixture.componentInstance.id).toBe(123);
    })
  
    it(&quot;should call populateForm when in edit mode&quot;, () =&gt; {
      spyOn(fixture.componentInstance, &quot;populateForm&quot;);
      fixture.detectChanges();
  
      expect(fixture.componentInstance.populateForm).toHaveBeenCalled();
    });
  </code></pre>
  <p>the first two tests are passing where isEdit mode is true and id is also set to 123 but the last test</p>
  <p><code>expect(fixture.componentInstance.populateForm).toHaveBeenCalled();</code> is not passing, i tried to use <code>fakeAsync</code>, <code>whenStable</code> and <code>flush()</code> but still the test is failing im not sure why?</p>
  <p>Thanks in advance</p>