testing route method in angular unit test

  angular, angular8, jasmin, karma-jasmine

I am working on angular application in which I am writing unit test. I have one method in my component which is as follows

MyComponent -
        route(element) {
         this.myService.Name(element.Name);
         this.myService.Code(element.Code);
         this.router.navigate(["myPage"], { relativeTo: this.routes});
      }
    
    MyService - 
    
         Name(data) {
            this.name.next(data);
          }
        
         Code(data) {
            this.code.next(data);
          }
       private code = new BehaviorSubject("");
       getCode = this.code.asObservable();
    
      private name= new BehaviorSubject("");
      getName = this.name.asObservable();

My unit test is as follows-

 it('route() function should be called', () => {
       component.route(mockData[0]);
       var spy = spyOn(component, "route").and.callThrough();
       expect(spy).toBeDefined();
   })

I am getting following error on running test case

TypeError: this.myService.Name is not a function

How can I resolve this?

Source: Angular Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.