auto-spies
auto-spies copied to clipboard
Must be called with should support specifying only some parameters.
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.
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' }))
Jest actually has the same functionality that @ThomasOrtiz mentioned:
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),
}),
);
});
thanks @ThomasOrtiz and @Brandon-Ritchie !
That's the way I'll solve it as well
closing unless you guys have a feature suggestion