Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing services documentation unclear #42748

Open
ajsaraujo opened this issue Jul 2, 2021 · 8 comments
Open

Testing services documentation unclear #42748

ajsaraujo opened this issue Jul 2, 2021 · 8 comments
Labels
comp: docs docsarea: tutorial P2
Milestone

Comments

@ajsaraujo
Copy link

@ajsaraujo ajsaraujo commented Jul 2, 2021

In the Testing Services documentation, a reference to the asyncData function is made, but the docs don't explain where this function is coming from.

image

@Foxandxss
Copy link
Member

@Foxandxss Foxandxss commented Jul 2, 2021

I checked it and it is true. Also I see complains about it here since 2018.

I tracked it back to here and it is marked as deprecated.

@aikidave we should probably swap that method for something else (perhaps a rxjs operator) and update the docs.

@atscott atscott added the comp: docs label Jul 2, 2021
@ngbot ngbot bot added this to the needsTriage milestone Jul 2, 2021
@flash-me
Copy link
Contributor

@flash-me flash-me commented Jul 14, 2021

A simple httpClientSpy.get.and.returnValue(of(expectedHeroes)); would be enough, or not?

cheers
flash

@Foxandxss
Copy link
Member

@Foxandxss Foxandxss commented Jul 14, 2021

A simple httpClientSpy.get.and.returnValue(of(expectedHeroes)); would be enough, or not?

cheers
flash

of is not async and httpClient.get is.

@flash-me
Copy link
Contributor

@flash-me flash-me commented Jul 14, 2021

httpClient.get

It's not. An Observable is returned, which doesn't make it async necessarily.
The async part comes from the raw http request behind the service.

UPDATE
Let's go through this.
We're mocking httpClient.get with of(expectedHeroes)
heroService.getHeroes() itself calls let's say just a return httpClient.get(asölkdfj)
In the Test there is this part

  heroService.getHeroes().subscribe(             // <-- Returns httpClient.get() <-- returns of(expectedHeroes)
    heroes => {  // <-- expectedHeroes
      expect(heroes).toEqual(expectedHeroes, 'expected heroes'); // <-- ????
      done();   // <-- Profit
    },
    done.fail
  );

Seems to be enough imo :-)

cheers
flash

@ajsaraujo
Copy link
Author

@ajsaraujo ajsaraujo commented Jul 15, 2021

It is enough. That's how I did it.

@AlixRoyere
Copy link

@AlixRoyere AlixRoyere commented Dec 13, 2021

Then if we take the example, and don't care about the heroes but only about the spy call. What should we do and why? I'm a bit confused about this async/not async.

it('should return expected heroes (HttpClient called once)', (done: DoneFn) => {
  const expectedHeroes: Hero[] =
    [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];

  httpClientSpy.get.and.returnValue(of(expectedHeroes));

  heroService.getHeroes().subscribe(
    heroes => {
      done();
    },
    done.fail
  );
  expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});
it('should return expected heroes (HttpClient called once)', () => {
  const expectedHeroes: Hero[] =
    [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];

  httpClientSpy.get.and.returnValue(of(expectedHeroes));

  heroService.getHeroes().subscribe();

  expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});
it('should return expected heroes (HttpClient called once)', (done: DoneFn) => {
  const expectedHeroes: Hero[] =
    [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];

  httpClientSpy.get.and.returnValue(of(expectedHeroes));

  heroService.getHeroes().subscribe().add(() => {

     expect(httpClientSpy.get.calls.count()).toBe(1, 'one call'); 

     done();
    }
});

@flash-me
Copy link
Contributor

@flash-me flash-me commented Dec 13, 2021

@AlixRoyere

of([some, values, here]) is always sync.

So

it('should return expected heroes (HttpClient called once)', () => {
  const expectedHeroes: Hero[] =
    [{ id: 1, name: 'A' }, { id: 2, name: 'B' }];

  httpClientSpy.get.and.returnValue(of(expectedHeroes));

  heroService.getHeroes().subscribe();

  expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
});

would be enough.

cheers
flash

@bob-watson bob-watson added docsarea: tutorial P2 labels Jun 28, 2022
@ngbot ngbot bot removed this from the needsTriage milestone Jun 28, 2022
@ngbot ngbot bot added this to the Backlog milestone Jun 28, 2022
@ngbot ngbot bot removed this from the needsTriage milestone Jun 28, 2022
@ngbot ngbot bot added this to the Backlog milestone Jun 28, 2022
@bob-watson
Copy link
Contributor

@bob-watson bob-watson commented Jun 28, 2022

Assigned P2 to update this after such a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp: docs docsarea: tutorial P2
Projects
None yet
Development

No branches or pull requests

6 participants