auto-spies icon indicating copy to clipboard operation
auto-spies copied to clipboard

Must be called with should support specifying only some parameters.

Open ChrTall opened this issue 2 years ago • 2 comments

When trying to test a HttpClient Spy I would like to use e.g: httpSpy.get.mustBeCalledWith(MyBasePath + 'api/myEndpoint', $$$AnyOptions$$$)

$$$AnyOptions$$$ can be any value. (Headers, Creadentials, ReportProgress etc.) If I use: httpSpy.get.mustBeCalledWith(MyBasePath + 'api/myEndpoint') It gives me an error because my service has default values for the options which are used when I do not specify options.

ChrTall avatar Mar 30 '22 15:03 ChrTall

I've gotten around similar issues in the past by using Jasmine's built-in helpers

  • jasmine.any(SOME_TYPE)
  • jasmine.objectContaining(OBJ_WITH_PROPERTIES).
  • jasmine.anything()

For example, you could probably do

httpSpy.get.mustBeCalledWith(MyBasePath + 'api/myEndpoint', jasmine.any(Object))

or if you care about the specific properites in the options

httpSpy.get.mustBeCalledWith(MyBasePath + 'api/myEndpoint', jasmine.objectContaining({ observe: 'body' }))

ThomasOrtiz avatar Jun 23 '22 18:06 ThomasOrtiz

Jest actually has the same functionality that @ThomasOrtiz mentioned:

From Jest documentation:

test('onPress gets called with the right thing', () => {
  const onPress = jest.fn();
  simulatePresses(onPress);
  expect(onPress).toBeCalledWith(
    expect.objectContaining({
      x: expect.any(Number),
      y: expect.any(Number),
    }),
  );
});

Brandon-Ritchie avatar Sep 07 '22 19:09 Brandon-Ritchie

thanks @ThomasOrtiz and @Brandon-Ritchie !

That's the way I'll solve it as well

closing unless you guys have a feature suggestion

shairez avatar Oct 05 '22 23:10 shairez