1

I want make a testing in my project and the function I want test is a nested function, there function in function

the function like this:

  ngOnInit() {
    this.getMenu()
  }

this is my test spec:

    describe('NavigationComponent', () => {
        let component: NavigationComponent;
        let fixture: ComponentFixture<NavigationComponent>;

        beforeEach(async(() => {
          TestBed.configureTestingModule({
            imports: [

            ],
            providers: [
              { provide: AuthenticationService, useClass: MockAuthenticationService }
            ]
          })
          .compileComponents();
        }));

        beforeEach(() => {
          fixture = TestBed.createComponent(NavigationComponent);
          //navComponent = TestBed.createComponent(NavigationComponent);
          component = fixture.componentInstance;
          fixture.detectChanges();
        });

  it('getMenu should called', ()=>{
        spyOn(component, 'getMenu');                  
        fixture.detectChanges();                      
        expect(component.getMenu).toHaveBeenCalled();
    })

It's work when I test ngOnInit, but I don't know how to test "getMenu"

thanks for the help

2 Answers 2

3

You can check if the method getMenu() was called or not when ngOnInit() ran.

What you have to do is spy on that method and then verify.

spyOn(compInstance, 'getMenu');                  //spy on getMenu
fixture.detectChanges();                         //calls ngOnInit() and updates dom
expect(compInstance.getMenu).toHaveBeenCalled(); //verify getMenu() invokation

If you want to test the logic of getMenu(), then you can directly call it from your test spec and then verify its output.

let res = compInstance.getMenu();
expect(res).toBe('pizza');
Sign up to request clarification or add additional context in comments.

5 Comments

I try it and I get this message <spyOn> : could not find an object to spy upon for getMenu() Usage: spyOn(<object>, <methodName>)
Can you please show your test spec? Better add that to your question.
I add my test spec
Comment out fixture.detectChanges() inside before each and check. Because when first detectChanges happens ngOnInit() gets called. So before spying, getMenu() will be called. To avoid this, you have to spy on any method before it is being called.
after I comment I still get that message: <spyOn> : could not find an object to spy upon for getMenu() Usage: spyOn(<object>, <methodName>)
-1

Add spyOn(component, 'getMenu'); in beforeEach block after you are assigning component variable.

The error is telling you that it is not getting the required method that you want to spy on.

PS- Too late for this answer, but maybe it will help someone else

2 Comments

That would not solve the issue - it would just test that the function was called, but not actually test the function itself
you can chain it with .and.callThrough() if you want to call the real function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.