Open Styrna opened 5 years ago
Hi @Styrna
Isn't: verify(fooMock.someMethod(expectedValue)).once();
what you need?
You don't need to use capture to verify arguments.
And if you want to check if objects are deeply equal: verify(fooMock.someMethod(deepEqual(expectedValue))).once();
Ok sorry i oversimplified the sample. What if i dont have expected value (like its created during medoth execution) so matchers are needed as i can only match its properties by lambda.
verify(fooMock.someMethod(anything())).once();
const callArg= capture(fooMock.someMethod).last()[0];
expect(callArg.Property.toBe(expectedValue);
so to simply capture invokation:
verifyArg(fooMock.someMethod, arg => expect(arg.Property.toBe(expectedValue)).once();
@Styrna ok in this case it would be nice to have matcher for example: verify(fooMock.someMethod(propsEqual(expectedValue))).once();
And expectedValue
will be object just with some properties. So not all object will be matched but just provided properties. With such kind of matcher, there is no need to create new verifyArg
function. It is important to keep API consistent.
Would you like to contribute and create such kind of matcher?
Love this library, was expecting to find something like this:
class AnyWhereMatcher<T> extends Matcher {
constructor(private predicate: (v: T) => boolean) {
super();
}
public match(value: any): boolean {
return this.predicate(value);
}
public toString(): string {
return `anyWhere(${this.predicate})`;
}
}
export function anyWhere<T>(predicate: (v: T) => boolean) {
return (new AnyWhereMatcher<T>(predicate) as any) as T;
}
Is it worth me raising a PR to add it?
Hi,
really like the lib for testing (kudos) but i find the writing matches for args quite complex (boiler-plate):
here is how its so far suggested (if i get it right):
i wrote a function for doing this (ofc generic) in one line like this:
verifyArg(fooMock.someMethod, arg => expect(callArg.toBe(expectedValue)).once();
here is function code:
here is full function with overloads for intelisense