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

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

问题描述

我正在尝试使用 e2e test angular 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);
    }
}

我的服务中还有另外两个服务,UrlDecoratorServiceAPIFetcherService.

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

这是我的 e2e 测试:

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

我需要的是如何将 UrlDecoratorServiceAPIFetcherService 这两个服务注入我的 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

构建

在您的应用程序上,然后您在本地托管(您也可以在外部进行)构建您刚刚创建的构建.

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

所以基本上你的服务包含在这些包中(在你的主包中更精确)所以你不需要设置任何配置而是开始使用量角器 API 测试你的应用程序 浏览器 等等.因此,在查看您的问题时,您的测试文件可能看起来更像这样:

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 测试:如何测试服务注入(使用)另一个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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