Poor Man’s Mocks in TypeScript

Posted by in Software, Tutorial

Today I went against my better judgement of usign a mock framework and decided to create my own mocks to allow coverage testing for an IPC-calling code.

My reference code I wanted to test is:

The code would use an angular service named PathService which, for the purpose of this test only has a method named getSourcePaths(), which returns a Promise. Now, I needed to emulate that method, and the code I wrote to do that is:

It basically defines an object containing a method getSourcePaths() which returns another object containing a method named then(fun:any) which accepts a parameter fun and calls it. Simple. In jargon, I’ve implemented an anonymous interface :)

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)