You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
5 Hrs
52 Min
13 Seg

Pruebas a promesas

16/23
Resources

Contributions 3

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Aqui ejecutaremos pruebas a promesas desde componentes para lo cual probaremos utilizando value service que tiene una funci贸n de llamada a una promesa

primero lo inyectaremos como dependencia

products.component.ts

import { ValueService } from './../../services/value.service';

constructor(
聽 聽 private valueService: ValueService,
聽 ) { }

async callPromise () {
聽 聽 const rta = await this.valueService.getPromiseValue();
聽 聽 this.rta = rta;
聽 }

luego lo llamaremos para hacer las pruebas

products.component.spec.ts

import { ValueService } from './../../services/value.service';

describe('ProductsComponent', () => {
聽 let valueService: jasmine.SpyObj<ValueService>;

聽 beforeEach(async () => {
聽 聽 const productServiceSpy = jasmine.createSpyObj('ProductsService', ['getAll']);
聽 聽 const valueServiceSpy = jasmine.createSpyObj('ValueService', ['getPromiseValue']);

聽 聽 await TestBed.configureTestingModule({
聽 聽 聽 declarations: [ ProductsComponent, ProductComponent ],
聽 聽 聽 providers: [
聽 聽 聽 聽 { provide: ProductsService, useValue: productServiceSpy },
聽 聽 聽 聽 { provide: ValueService, useValue: valueServiceSpy }
聽 聽 聽 ]
聽 聽 })
聽 聽 .compileComponents();
聽 });
聽
beforeEach(() => {
聽 聽 fixture = TestBed.createComponent(ProductsComponent);
聽 聽 component = fixture.componentInstance;
聽 聽 productService = TestBed.inject(ProductsService) as jasmine.SpyObj<ProductsService>;
聽 聽 valueService = TestBed.inject(ValueService) as jasmine.SpyObj<ValueService>;

聽 聽 const productsMock = generateManyProducts(3);
聽 聽 productService.getAll.and.returnValue(of(productsMock));
聽 聽 fixture.detectChanges(); //ngOnInit()
聽 });


describe('test for callPromise', () => {
聽 聽 it('should call to promise', async() => {
聽 聽 聽 // Arrange
聽 聽 聽 const mockMsg = 'my mock string';

聽 聽 聽 valueService.getPromiseValue.and.returnValue(Promise.resolve(mockMsg));
聽 聽 聽 // Act
聽 聽 聽 await component.callPromise();
聽 聽 聽 fixture.detectChanges();
聽 聽 聽 // Assert
聽 聽 聽 expect(component.rta).toEqual(mockMsg);
聽 聽 聽 expect(valueService.getPromiseValue).toHaveBeenCalled();
聽 聽 });
No lograba hacer funcionar este test, siempre fallaba porque quer铆a suscribirse a algo que no estaba definido. Entonces ented铆 que al ejecutar `fixture.detectChanges()` estaba ejecutando el ciclo de vida del componente, por lo que se estaba ejecuntando el `ngOnInit` del componente `Products`. En el `ngOnInit`, este componente llama a `getAllProducts()` y ah铆 es donde se genera esta suscripci贸n. Por lo tanto para solucionar las fallas en este test tuve que a帽adir tambi茅n un esp铆a para el `productsService` con un mock de productos.
Entonces mi duda es, 驴porqu茅 debemos usar fakeAsync() y tick, si podemos hacerlo directamente con Async() y await? o es que esto 煤ltimo es solo v谩lido para promesas y lo otro para observables y setTimeout(), no me qued贸 claro del todo...