typemoq icon indicating copy to clipboard operation
typemoq copied to clipboard

Creating custom matches

Open mindlink opened this issue 8 years ago • 1 comments

Howdy,

We have been widely using TypeMoq as our chosen typescript mocking library and it's worked well for us! Thank you for all your hard work on the library!

Unfortunately we've frozen on the 0.6 release because we implement some custom Matches and as you know in order to successfully implement a custom Match you need to expose the certain properties that have the value of some internal constants.

Newer versions of typemoq don't expose this publicly, so it's a pain to implement a matcher (still possible, but not a great development experience).

I understand that there is a predicate matcher, and it's thus possible to implement a matcher using that, however it makes sense from an extensibility stand point to enable developers to create their own matchers should they find an interesting case that isn't directly supported.

Any enlightenment on the design decision would be welcome, or perhaps I am mistaken and you can do this - in which case how? :)

mindlink avatar Sep 28 '17 15:09 mindlink

I managed to get one working:

import { IMatch } from 'typemoq/Match/IMatch';

export class Itx {
	public static isAnyFunction<T>(): T {
		let matcher = new MatchAnyFunc();
		return <any>matcher;
	}
}

class MatchAnyFunc implements IMatch {
	public readonly ___id = '438A51D3-6864-49D7-A655-CA1153B86965';

	public ___matches(object: object): boolean {
		return object && {}.toString.call(object) === '[object Function]';
	}
	public toString(): string {
		let res = `It.isAnyFunction()`;
		return res;
	}
}
mock.verify(m => m.log(Itx.isAnyFunction()), Times.once());

SeanSobey avatar Mar 27 '18 07:03 SeanSobey