spyOn not able to spy on the method of a dependent service











up vote
0
down vote

favorite












I am testing AuthService which sends user login info to server using another HelperService.



public authServiceSigninUser(user:UserSigninInfo):any{
console.log('In authServiceSigninUser. contacting server at '+this.API_URL +this.SIGNIN_USER_URL +" with user data "+user+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers ); //TODOM password should be sent in encrypted format.

let signinInfo= new UserSigninAPI(user);
let body = JSON.stringify(signinInfo);
return this.helperService.sendMessage(this.SIGNIN_USER_URL,body,httpOptions)
}


I am trying to test the authServiceSigninUser method as follows but when I run the spec, I get error Error: <spyOn> : sendMessage() method does not exist
Usage: spyOn(<object>, <methodName>)
. Why?



describe('authServiceSigninUser test suite',()=>{
beforeEach(()=>{
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AuthService, HelperService]
});
});
fit('should sign in user',()=>{
let spy:any;
let helper = TestBed.get(HelperService);
let authService = TestBed.get(AuthService);
let userSignIn = new UserSigninInfo("test@test.com","test");
let httpMock = TestBed.get(HttpTestingController);
spyOn(helper.sendMessage,'sendMessage');
let observable:Observable<HttpEvent<any>> = authService.authServiceSigninUser(userSignIn);
let subscription = observable.subscribe((event)=>{
console.log('event from authService',event);
});
const responseData = { result: 'success', ['additional-info']: 'login success' };
let httpEvent:HttpResponse<any> = new HttpResponse<any>({body:responseData});
expect(helper.sendMessage).toHaveBeenCalled();//ERROR here
const mockReq:TestRequest = httpMock.expectOne(environment.apiUrl+environment.signinUserUrl); //Expect that a single request has been made which matches the given URL, and return its mock
//once mocking of sending request is done, mock receiving a response. This will trigger the logic inside subscribe function
mockReq.flush(httpEvent); //flush method provides dummy values as response
httpMock.verify();//verify checks that there are no outstanding requests;

});
});









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am testing AuthService which sends user login info to server using another HelperService.



    public authServiceSigninUser(user:UserSigninInfo):any{
    console.log('In authServiceSigninUser. contacting server at '+this.API_URL +this.SIGNIN_USER_URL +" with user data "+user+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers ); //TODOM password should be sent in encrypted format.

    let signinInfo= new UserSigninAPI(user);
    let body = JSON.stringify(signinInfo);
    return this.helperService.sendMessage(this.SIGNIN_USER_URL,body,httpOptions)
    }


    I am trying to test the authServiceSigninUser method as follows but when I run the spec, I get error Error: <spyOn> : sendMessage() method does not exist
    Usage: spyOn(<object>, <methodName>)
    . Why?



    describe('authServiceSigninUser test suite',()=>{
    beforeEach(()=>{
    TestBed.configureTestingModule({
    imports: [HttpClientTestingModule],
    providers: [AuthService, HelperService]
    });
    });
    fit('should sign in user',()=>{
    let spy:any;
    let helper = TestBed.get(HelperService);
    let authService = TestBed.get(AuthService);
    let userSignIn = new UserSigninInfo("test@test.com","test");
    let httpMock = TestBed.get(HttpTestingController);
    spyOn(helper.sendMessage,'sendMessage');
    let observable:Observable<HttpEvent<any>> = authService.authServiceSigninUser(userSignIn);
    let subscription = observable.subscribe((event)=>{
    console.log('event from authService',event);
    });
    const responseData = { result: 'success', ['additional-info']: 'login success' };
    let httpEvent:HttpResponse<any> = new HttpResponse<any>({body:responseData});
    expect(helper.sendMessage).toHaveBeenCalled();//ERROR here
    const mockReq:TestRequest = httpMock.expectOne(environment.apiUrl+environment.signinUserUrl); //Expect that a single request has been made which matches the given URL, and return its mock
    //once mocking of sending request is done, mock receiving a response. This will trigger the logic inside subscribe function
    mockReq.flush(httpEvent); //flush method provides dummy values as response
    httpMock.verify();//verify checks that there are no outstanding requests;

    });
    });









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am testing AuthService which sends user login info to server using another HelperService.



      public authServiceSigninUser(user:UserSigninInfo):any{
      console.log('In authServiceSigninUser. contacting server at '+this.API_URL +this.SIGNIN_USER_URL +" with user data "+user+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers ); //TODOM password should be sent in encrypted format.

      let signinInfo= new UserSigninAPI(user);
      let body = JSON.stringify(signinInfo);
      return this.helperService.sendMessage(this.SIGNIN_USER_URL,body,httpOptions)
      }


      I am trying to test the authServiceSigninUser method as follows but when I run the spec, I get error Error: <spyOn> : sendMessage() method does not exist
      Usage: spyOn(<object>, <methodName>)
      . Why?



      describe('authServiceSigninUser test suite',()=>{
      beforeEach(()=>{
      TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [AuthService, HelperService]
      });
      });
      fit('should sign in user',()=>{
      let spy:any;
      let helper = TestBed.get(HelperService);
      let authService = TestBed.get(AuthService);
      let userSignIn = new UserSigninInfo("test@test.com","test");
      let httpMock = TestBed.get(HttpTestingController);
      spyOn(helper.sendMessage,'sendMessage');
      let observable:Observable<HttpEvent<any>> = authService.authServiceSigninUser(userSignIn);
      let subscription = observable.subscribe((event)=>{
      console.log('event from authService',event);
      });
      const responseData = { result: 'success', ['additional-info']: 'login success' };
      let httpEvent:HttpResponse<any> = new HttpResponse<any>({body:responseData});
      expect(helper.sendMessage).toHaveBeenCalled();//ERROR here
      const mockReq:TestRequest = httpMock.expectOne(environment.apiUrl+environment.signinUserUrl); //Expect that a single request has been made which matches the given URL, and return its mock
      //once mocking of sending request is done, mock receiving a response. This will trigger the logic inside subscribe function
      mockReq.flush(httpEvent); //flush method provides dummy values as response
      httpMock.verify();//verify checks that there are no outstanding requests;

      });
      });









      share|improve this question













      I am testing AuthService which sends user login info to server using another HelperService.



      public authServiceSigninUser(user:UserSigninInfo):any{
      console.log('In authServiceSigninUser. contacting server at '+this.API_URL +this.SIGNIN_USER_URL +" with user data "+user+ " with httpOptions "+httpOptions.withCredentials + ","+httpOptions.headers ); //TODOM password should be sent in encrypted format.

      let signinInfo= new UserSigninAPI(user);
      let body = JSON.stringify(signinInfo);
      return this.helperService.sendMessage(this.SIGNIN_USER_URL,body,httpOptions)
      }


      I am trying to test the authServiceSigninUser method as follows but when I run the spec, I get error Error: <spyOn> : sendMessage() method does not exist
      Usage: spyOn(<object>, <methodName>)
      . Why?



      describe('authServiceSigninUser test suite',()=>{
      beforeEach(()=>{
      TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [AuthService, HelperService]
      });
      });
      fit('should sign in user',()=>{
      let spy:any;
      let helper = TestBed.get(HelperService);
      let authService = TestBed.get(AuthService);
      let userSignIn = new UserSigninInfo("test@test.com","test");
      let httpMock = TestBed.get(HttpTestingController);
      spyOn(helper.sendMessage,'sendMessage');
      let observable:Observable<HttpEvent<any>> = authService.authServiceSigninUser(userSignIn);
      let subscription = observable.subscribe((event)=>{
      console.log('event from authService',event);
      });
      const responseData = { result: 'success', ['additional-info']: 'login success' };
      let httpEvent:HttpResponse<any> = new HttpResponse<any>({body:responseData});
      expect(helper.sendMessage).toHaveBeenCalled();//ERROR here
      const mockReq:TestRequest = httpMock.expectOne(environment.apiUrl+environment.signinUserUrl); //Expect that a single request has been made which matches the given URL, and return its mock
      //once mocking of sending request is done, mock receiving a response. This will trigger the logic inside subscribe function
      mockReq.flush(httpEvent); //flush method provides dummy values as response
      httpMock.verify();//verify checks that there are no outstanding requests;

      });
      });






      angular6 angular-test angular-testing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 17 at 14:26









      Manu Chadha

      2,49411235




      2,49411235
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          It seems the correct way is spyOn(helper,'sendMessage');, not spyOn(helper.sendMessage,'sendMessage');






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352134%2fspyon-not-able-to-spy-on-the-method-of-a-dependent-service%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            It seems the correct way is spyOn(helper,'sendMessage');, not spyOn(helper.sendMessage,'sendMessage');






            share|improve this answer

























              up vote
              0
              down vote













              It seems the correct way is spyOn(helper,'sendMessage');, not spyOn(helper.sendMessage,'sendMessage');






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                It seems the correct way is spyOn(helper,'sendMessage');, not spyOn(helper.sendMessage,'sendMessage');






                share|improve this answer












                It seems the correct way is spyOn(helper,'sendMessage');, not spyOn(helper.sendMessage,'sendMessage');







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 at 14:54









                Manu Chadha

                2,49411235




                2,49411235






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53352134%2fspyon-not-able-to-spy-on-the-method-of-a-dependent-service%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin