angular e2e testing : how to test Service inject(use) another services












0















i'm trying to test my service with e2e test angular 7, my problem is i don't know how to do that:



it's my service, (the methode return Observable):



import { Injectable } from '@angular/core';
import { UrlDecoratorService } from "../../common/url-decorator.service";
import { APIFetcherService } from "../common/api-fetcher.service";
import { Observable } from 'rxjs';
import { IALChrono, ALChrono } from '../../common/IALChrono.interface';

@Injectable()
export class AnnonceChronoDetailService {
private months: string;
constructor(private urlDecoratorService: UrlDecoratorService, private apiFetcher: APIFetcherService) {
}

fetchData(chronoInfo: ALChrono): Observable<any> {
// construct API parameters and URL
var URL: string = this.urlDecoratorService.urlAPIDecorate("AL", "GetAccessChrono");

var params = this.urlDecoratorService.generateParameters({
year: chronoInfo.year,
month: chronoInfo.month,
sortBy: chronoInfo.sortBy,
sortDirection: chronoInfo.sortDirection,
pageNumber: chronoInfo.currentPage,
pageSize: chronoInfo.pageSize
});

return this.apiFetcher.fetchJson(URL, params);
}
}




it have two other services inside my service, UrlDecoratorService and APIFetcherService.



this is my e2e test:



import { AppPage } from './app.po';
import { AnnonceChronoDetailService } from '../../src/app/services/annonce-legale/annonce-chrono-detail.service';
import { ALChrono } from '../../src/app/common/IALChrono.interface';
import { APIResponse } from '../../src/app/common/api-response.interface';
import { Observable } from 'rxjs';

describe('workspace-project App', () => {
let page: AppPage;
let service: AnnonceChronoDetailService;
this.chronoInfo = new ALChrono(); //it's a class

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
});


it('#getObservableValue should return value from observable', (done: DoneFn) => {
service.fetchData(this.chronoInfo).subscribe((resp: APIResponse) => {
expect(resp.dataCount).toBe(5);
done();
});
});
});




what i need is how to inject the two services UrlDecoratorService and APIFetcherService to my e2e test, or how to test services that inject another services?



if you need more informations please tell me.










share|improve this question

























  • Your services should already be available when running the e2e, update the post with error you are getting. What exactly do you want to test? If it's the actual services you want to test you should not be doing e2e tests, instead do unit-tests on them

    – Lucho
    Nov 18 '18 at 8:47













  • @Lucho this is my first post, i want to test the real data returned by the service.

    – saad
    Nov 19 '18 at 12:00











  • So you have a real dev. application hosted from a server already then? If so you can run Protractor straight to that server through to url and test it live with Protractor

    – Lucho
    Nov 19 '18 at 19:44











  • do you mean e2e testing?

    – saad
    Nov 21 '18 at 9:02











  • because i try this, see my post please

    – saad
    Nov 21 '18 at 9:07
















0















i'm trying to test my service with e2e test angular 7, my problem is i don't know how to do that:



it's my service, (the methode return Observable):



import { Injectable } from '@angular/core';
import { UrlDecoratorService } from "../../common/url-decorator.service";
import { APIFetcherService } from "../common/api-fetcher.service";
import { Observable } from 'rxjs';
import { IALChrono, ALChrono } from '../../common/IALChrono.interface';

@Injectable()
export class AnnonceChronoDetailService {
private months: string;
constructor(private urlDecoratorService: UrlDecoratorService, private apiFetcher: APIFetcherService) {
}

fetchData(chronoInfo: ALChrono): Observable<any> {
// construct API parameters and URL
var URL: string = this.urlDecoratorService.urlAPIDecorate("AL", "GetAccessChrono");

var params = this.urlDecoratorService.generateParameters({
year: chronoInfo.year,
month: chronoInfo.month,
sortBy: chronoInfo.sortBy,
sortDirection: chronoInfo.sortDirection,
pageNumber: chronoInfo.currentPage,
pageSize: chronoInfo.pageSize
});

return this.apiFetcher.fetchJson(URL, params);
}
}




it have two other services inside my service, UrlDecoratorService and APIFetcherService.



this is my e2e test:



import { AppPage } from './app.po';
import { AnnonceChronoDetailService } from '../../src/app/services/annonce-legale/annonce-chrono-detail.service';
import { ALChrono } from '../../src/app/common/IALChrono.interface';
import { APIResponse } from '../../src/app/common/api-response.interface';
import { Observable } from 'rxjs';

describe('workspace-project App', () => {
let page: AppPage;
let service: AnnonceChronoDetailService;
this.chronoInfo = new ALChrono(); //it's a class

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
});


it('#getObservableValue should return value from observable', (done: DoneFn) => {
service.fetchData(this.chronoInfo).subscribe((resp: APIResponse) => {
expect(resp.dataCount).toBe(5);
done();
});
});
});




what i need is how to inject the two services UrlDecoratorService and APIFetcherService to my e2e test, or how to test services that inject another services?



if you need more informations please tell me.










share|improve this question

























  • Your services should already be available when running the e2e, update the post with error you are getting. What exactly do you want to test? If it's the actual services you want to test you should not be doing e2e tests, instead do unit-tests on them

    – Lucho
    Nov 18 '18 at 8:47













  • @Lucho this is my first post, i want to test the real data returned by the service.

    – saad
    Nov 19 '18 at 12:00











  • So you have a real dev. application hosted from a server already then? If so you can run Protractor straight to that server through to url and test it live with Protractor

    – Lucho
    Nov 19 '18 at 19:44











  • do you mean e2e testing?

    – saad
    Nov 21 '18 at 9:02











  • because i try this, see my post please

    – saad
    Nov 21 '18 at 9:07














0












0








0








i'm trying to test my service with e2e test angular 7, my problem is i don't know how to do that:



it's my service, (the methode return Observable):



import { Injectable } from '@angular/core';
import { UrlDecoratorService } from "../../common/url-decorator.service";
import { APIFetcherService } from "../common/api-fetcher.service";
import { Observable } from 'rxjs';
import { IALChrono, ALChrono } from '../../common/IALChrono.interface';

@Injectable()
export class AnnonceChronoDetailService {
private months: string;
constructor(private urlDecoratorService: UrlDecoratorService, private apiFetcher: APIFetcherService) {
}

fetchData(chronoInfo: ALChrono): Observable<any> {
// construct API parameters and URL
var URL: string = this.urlDecoratorService.urlAPIDecorate("AL", "GetAccessChrono");

var params = this.urlDecoratorService.generateParameters({
year: chronoInfo.year,
month: chronoInfo.month,
sortBy: chronoInfo.sortBy,
sortDirection: chronoInfo.sortDirection,
pageNumber: chronoInfo.currentPage,
pageSize: chronoInfo.pageSize
});

return this.apiFetcher.fetchJson(URL, params);
}
}




it have two other services inside my service, UrlDecoratorService and APIFetcherService.



this is my e2e test:



import { AppPage } from './app.po';
import { AnnonceChronoDetailService } from '../../src/app/services/annonce-legale/annonce-chrono-detail.service';
import { ALChrono } from '../../src/app/common/IALChrono.interface';
import { APIResponse } from '../../src/app/common/api-response.interface';
import { Observable } from 'rxjs';

describe('workspace-project App', () => {
let page: AppPage;
let service: AnnonceChronoDetailService;
this.chronoInfo = new ALChrono(); //it's a class

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
});


it('#getObservableValue should return value from observable', (done: DoneFn) => {
service.fetchData(this.chronoInfo).subscribe((resp: APIResponse) => {
expect(resp.dataCount).toBe(5);
done();
});
});
});




what i need is how to inject the two services UrlDecoratorService and APIFetcherService to my e2e test, or how to test services that inject another services?



if you need more informations please tell me.










share|improve this question
















i'm trying to test my service with e2e test angular 7, my problem is i don't know how to do that:



it's my service, (the methode return Observable):



import { Injectable } from '@angular/core';
import { UrlDecoratorService } from "../../common/url-decorator.service";
import { APIFetcherService } from "../common/api-fetcher.service";
import { Observable } from 'rxjs';
import { IALChrono, ALChrono } from '../../common/IALChrono.interface';

@Injectable()
export class AnnonceChronoDetailService {
private months: string;
constructor(private urlDecoratorService: UrlDecoratorService, private apiFetcher: APIFetcherService) {
}

fetchData(chronoInfo: ALChrono): Observable<any> {
// construct API parameters and URL
var URL: string = this.urlDecoratorService.urlAPIDecorate("AL", "GetAccessChrono");

var params = this.urlDecoratorService.generateParameters({
year: chronoInfo.year,
month: chronoInfo.month,
sortBy: chronoInfo.sortBy,
sortDirection: chronoInfo.sortDirection,
pageNumber: chronoInfo.currentPage,
pageSize: chronoInfo.pageSize
});

return this.apiFetcher.fetchJson(URL, params);
}
}




it have two other services inside my service, UrlDecoratorService and APIFetcherService.



this is my e2e test:



import { AppPage } from './app.po';
import { AnnonceChronoDetailService } from '../../src/app/services/annonce-legale/annonce-chrono-detail.service';
import { ALChrono } from '../../src/app/common/IALChrono.interface';
import { APIResponse } from '../../src/app/common/api-response.interface';
import { Observable } from 'rxjs';

describe('workspace-project App', () => {
let page: AppPage;
let service: AnnonceChronoDetailService;
this.chronoInfo = new ALChrono(); //it's a class

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
});


it('#getObservableValue should return value from observable', (done: DoneFn) => {
service.fetchData(this.chronoInfo).subscribe((resp: APIResponse) => {
expect(resp.dataCount).toBe(5);
done();
});
});
});




what i need is how to inject the two services UrlDecoratorService and APIFetcherService to my e2e test, or how to test services that inject another services?



if you need more informations please tell me.







angular protractor e2e-testing angular-test angular-e2e






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 14:28







saad

















asked Nov 16 '18 at 12:11









saadsaad

195




195













  • Your services should already be available when running the e2e, update the post with error you are getting. What exactly do you want to test? If it's the actual services you want to test you should not be doing e2e tests, instead do unit-tests on them

    – Lucho
    Nov 18 '18 at 8:47













  • @Lucho this is my first post, i want to test the real data returned by the service.

    – saad
    Nov 19 '18 at 12:00











  • So you have a real dev. application hosted from a server already then? If so you can run Protractor straight to that server through to url and test it live with Protractor

    – Lucho
    Nov 19 '18 at 19:44











  • do you mean e2e testing?

    – saad
    Nov 21 '18 at 9:02











  • because i try this, see my post please

    – saad
    Nov 21 '18 at 9:07



















  • Your services should already be available when running the e2e, update the post with error you are getting. What exactly do you want to test? If it's the actual services you want to test you should not be doing e2e tests, instead do unit-tests on them

    – Lucho
    Nov 18 '18 at 8:47













  • @Lucho this is my first post, i want to test the real data returned by the service.

    – saad
    Nov 19 '18 at 12:00











  • So you have a real dev. application hosted from a server already then? If so you can run Protractor straight to that server through to url and test it live with Protractor

    – Lucho
    Nov 19 '18 at 19:44











  • do you mean e2e testing?

    – saad
    Nov 21 '18 at 9:02











  • because i try this, see my post please

    – saad
    Nov 21 '18 at 9:07

















Your services should already be available when running the e2e, update the post with error you are getting. What exactly do you want to test? If it's the actual services you want to test you should not be doing e2e tests, instead do unit-tests on them

– Lucho
Nov 18 '18 at 8:47







Your services should already be available when running the e2e, update the post with error you are getting. What exactly do you want to test? If it's the actual services you want to test you should not be doing e2e tests, instead do unit-tests on them

– Lucho
Nov 18 '18 at 8:47















@Lucho this is my first post, i want to test the real data returned by the service.

– saad
Nov 19 '18 at 12:00





@Lucho this is my first post, i want to test the real data returned by the service.

– saad
Nov 19 '18 at 12:00













So you have a real dev. application hosted from a server already then? If so you can run Protractor straight to that server through to url and test it live with Protractor

– Lucho
Nov 19 '18 at 19:44





So you have a real dev. application hosted from a server already then? If so you can run Protractor straight to that server through to url and test it live with Protractor

– Lucho
Nov 19 '18 at 19:44













do you mean e2e testing?

– saad
Nov 21 '18 at 9:02





do you mean e2e testing?

– saad
Nov 21 '18 at 9:02













because i try this, see my post please

– saad
Nov 21 '18 at 9:07





because i try this, see my post please

– saad
Nov 21 '18 at 9:07












1 Answer
1






active

oldest

votes


















1














So the purpose of the e2e testing is to actually try out your app in it's build form, basically you have done a




ng build




on your application where you then afterwards host locally(you can do it externally aswell) that build you just created.



So basically you service is included in those bundles(more precise in your main bundle) so you dont need to setup any configs instead start testing your app with the protractor api with browser and so on.
So your test file could look more like this on when looking at your question:



  describe('workspace-project App', () => {
let page: AppPage;
this.chronoInfo = new ALChrono(); //it's a class

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
});
}


So the code including the service test should include in your unit-testing
If you are not sure how protractor works I suggest you to read up more on the documentation on how the setup works.






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',
    autoActivateHeartbeat: false,
    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%2f53337670%2fangular-e2e-testing-how-to-test-service-injectuse-another-services%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









    1














    So the purpose of the e2e testing is to actually try out your app in it's build form, basically you have done a




    ng build




    on your application where you then afterwards host locally(you can do it externally aswell) that build you just created.



    So basically you service is included in those bundles(more precise in your main bundle) so you dont need to setup any configs instead start testing your app with the protractor api with browser and so on.
    So your test file could look more like this on when looking at your question:



      describe('workspace-project App', () => {
    let page: AppPage;
    this.chronoInfo = new ALChrono(); //it's a class

    beforeEach(() => {
    page = new AppPage();
    });

    it('should display welcome message', () => {
    page.navigateTo();
    expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
    });
    }


    So the code including the service test should include in your unit-testing
    If you are not sure how protractor works I suggest you to read up more on the documentation on how the setup works.






    share|improve this answer




























      1














      So the purpose of the e2e testing is to actually try out your app in it's build form, basically you have done a




      ng build




      on your application where you then afterwards host locally(you can do it externally aswell) that build you just created.



      So basically you service is included in those bundles(more precise in your main bundle) so you dont need to setup any configs instead start testing your app with the protractor api with browser and so on.
      So your test file could look more like this on when looking at your question:



        describe('workspace-project App', () => {
      let page: AppPage;
      this.chronoInfo = new ALChrono(); //it's a class

      beforeEach(() => {
      page = new AppPage();
      });

      it('should display welcome message', () => {
      page.navigateTo();
      expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
      });
      }


      So the code including the service test should include in your unit-testing
      If you are not sure how protractor works I suggest you to read up more on the documentation on how the setup works.






      share|improve this answer


























        1












        1








        1







        So the purpose of the e2e testing is to actually try out your app in it's build form, basically you have done a




        ng build




        on your application where you then afterwards host locally(you can do it externally aswell) that build you just created.



        So basically you service is included in those bundles(more precise in your main bundle) so you dont need to setup any configs instead start testing your app with the protractor api with browser and so on.
        So your test file could look more like this on when looking at your question:



          describe('workspace-project App', () => {
        let page: AppPage;
        this.chronoInfo = new ALChrono(); //it's a class

        beforeEach(() => {
        page = new AppPage();
        });

        it('should display welcome message', () => {
        page.navigateTo();
        expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
        });
        }


        So the code including the service test should include in your unit-testing
        If you are not sure how protractor works I suggest you to read up more on the documentation on how the setup works.






        share|improve this answer













        So the purpose of the e2e testing is to actually try out your app in it's build form, basically you have done a




        ng build




        on your application where you then afterwards host locally(you can do it externally aswell) that build you just created.



        So basically you service is included in those bundles(more precise in your main bundle) so you dont need to setup any configs instead start testing your app with the protractor api with browser and so on.
        So your test file could look more like this on when looking at your question:



          describe('workspace-project App', () => {
        let page: AppPage;
        this.chronoInfo = new ALChrono(); //it's a class

        beforeEach(() => {
        page = new AppPage();
        });

        it('should display welcome message', () => {
        page.navigateTo();
        expect(page.getParagraphText()).toEqual('Welcome to MyProject!');
        });
        }


        So the code including the service test should include in your unit-testing
        If you are not sure how protractor works I suggest you to read up more on the documentation on how the setup works.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 23:08









        LuchoLucho

        239213




        239213






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337670%2fangular-e2e-testing-how-to-test-service-injectuse-another-services%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()