Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

protractor - e2e testing angular 7: Failed: Cannot read property 'fetchData' of undefined

I'm trying to do some e2e testing for my service in angular 7, the method return Observable, this is my methode:

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

and this is my 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();
    });
  });
});

and this is my terminal error :

DevTools listening on ws://127.0.0.1:53112/devtools/browser/2a1d94b2-ef47-4910-9ee2-e875b615ed45
Jasmine started

  workspace-project App
    √ should display welcome message
    × #getObservableValue should return value from observable
      - Failed: Cannot read property 'fetchData' of undefined
          at UserContext.<anonymous> (D:Fronte2esrcapp.e2e-spec.ts:34:13)
          at new ManagedPromise (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:1077:7)
          at ControlFlow.promise (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:2505:12)
          at TaskQueue.execute_ (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:3084:14)
          at TaskQueue.executeNext_ (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:3067:27)
          at asyncRun (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:2974:25)
          at D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:668:7
          at <anonymous>
      From: Task: Run it("#getObservableValue should return value from observable") in control flow
          at ControlFlow.emit (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibevents.js:62:21)
          at ControlFlow.shutdown_ (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:2674:10)
          at shutdownTask_.MicroTask (D:Front
ode_modulesprotractor
ode_modulesselenium-webdriverlibpromise.js:2599:53)
      From asynchronous test:
      Error
          at Suite.<anonymous> (D:Fronte2esrcapp.e2e-spec.ts:33:3)
          at Object.<anonymous> (D:Fronte2esrcapp.e2e-spec.ts:7:1)
          at Module._compile (module.js:635:30)
          at Module.m._compile (D:Front
ode_moduless-nodesrcindex.ts:439:23)
          at Module._extensions..js (module.js:646:10)
          at Object.require.extensions.(anonymous function) [as .ts] (D:Front
ode_moduless-nodesrcindex.ts:442:12)

**************************************************
*                    Failures                    *
**************************************************

1) workspace-project App #getObservableValue should return value from observable
  - Failed: Cannot read property 'fetchData' of undefined

Executed 2 of 2 specs (1 FAILED) in 6 secs.
[10:02:20] I/launcher - 0 instance(s) of WebDriver still running
[10:02:20] I/launcher - chrome #01 failed 1 test(s)
[10:02:20] I/launcher - overall: 1 failed spec(s)
[10:02:20] E/launcher - Process exited with error code 1
An unexpected error occurred: undefined

i want to test the real value of my return methode not like the unit testing, and they don't know my method fetchData.

if you need some more information please tell me??.

thanks a lot.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have not created an instance of AnnonceChronoDetailService (just the reference is created), that is why service variable is undefined

let page: AppPage;
let service: AnnonceChronoDetailService;
this.chronoInfo = new ALChrono(); //it's a class

beforeEach(() => {
    page = new AppPage();
    service = new AnnonceChronoDetailService(); // create service instance
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...