allenhwkim / ngentest

Angular6+ Unit Test Generator For Components, Directive, Services, and Pipes
https://ngentest.github.io
MIT License
144 stars 60 forks source link

[QUESTION] How to mock properly #74

Open NoXeoZ opened 5 months ago

NoXeoZ commented 5 months ago

Hi, i'm trying to make the library work on a angular project with test in karma framework

  1. gentest seems to search for a ngentest.config.js in the current directory, not in my appdata node module folder.
  2. When i generate the unit test from this class

import { Component, Input } from '@angular/core'; import { FiltreLocalStorageService } from 'src/app/service/filter-local-storage/filter-local-storage.service'; import { FiltreDataService } from 'src/app/service/filtre-data/filtre-data.service'; import { SecurityService } from 'src/app/service/security/security.service';

@Component({ selector: 'app-filtre-numero-serie', templateUrl: './filtre-numero-serie.component.html', styleUrls: ['./filtre-numero-serie.component.scss'] }) export class FiltreNumeroSerieComponent {

@Input() vue: string;

numeroSerie: string = '' placeholder: string = "Numéro de série"

constructor(private filtreDataService: FiltreDataService,private storageService :FiltreLocalStorageService,private securityService : SecurityService) { }

onKeyEnter(event: any) { this.filtreDataService.updateFilter(this.vue, { numeroSerie: this.numeroSerie.trim() }); this.securityService.logUserActivityObservable('Vue Borne, utilisation du filtre: matricule valeur ' + this.numeroSerie) }

getNumeroSerie(){ this.filtreDataService.updateFilter(this.vue, { numeroSerie: this.numeroSerie.trim() }); }

ngOnInit(){ let filtresInStorage = this.storageService.getFiltres(this.vue) this.numeroSerie = filtresInStorage ? filtresInStorage.numSerie : '' this.filtreDataService.subscribeToVue(this.vue,(filtreData) => this.numeroSerie = filtreData.numeroSerie) }

}

I got this test generate

// @ts-nocheck import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { Pipe, PipeTransform, Injectable, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, Directive, Input, Output } from '@angular/core'; import { isPlatformBrowser } from '@angular/common'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { By } from '@angular/platform-browser'; import { Observable, of as observableOf, throwError } from 'rxjs';

import { Component } from '@angular/core'; import { FiltreNumeroSerieComponent } from './filtre-numero-serie.component'; import { FiltreDataService } from 'src/app/service/filtre-data/filtre-data.service'; import { FiltreLocalStorageService } from 'src/app/service/filter-local-storage/filter-local-storage.service'; import { SecurityService } from 'src/app/service/security/security.service';

@Injectable() class MockFiltreDataService {}

@Injectable() class MockFiltreLocalStorageService {}

@Injectable() class MockSecurityService {}

@Directive({ selector: '[myCustom]' }) class MyCustomDirective { @Input() myCustom; }

.....

Mock are not generated properly, how can i config that ?

Thanks