角度e2e测试:如何测试Service注入(使用)其他服务 [英] angular e2e testing : how to test Service inject(use) another services

查看:183
本文介绍了角度e2e测试:如何测试Service注入(使用)其他服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用e2e测试角度7来测试我的服务,我的问题是我不知道该怎么做:

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

这是我的服务,(方法返回Observable):

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);
    }
}

它在我的服务中还有另外两个服务, UrlDecoratorService APIFetcherService .

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

这是我的e2e测试:

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();
    });
  });
});

我需要的是如何将两个服务 UrlDecoratorService APIFetcherService 注入我的e2e测试,或者如何测试注入其他服务的服务?

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.

推荐答案

所以e2e测试的目的是在构建形式中实际试用您的应用程序,基本上您已经完成了

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构建

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.

这篇关于角度e2e测试:如何测试Service注入(使用)其他服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆