Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  26] [ 2]  / answers: 1 / hits: 47897  / 8 Years ago, wed, may 4, 2016, 12:00:00

I work with Typescript on an AngularJS 1.X project. I use different Javascript libraries for different purposes. To unit-test my source I would like to stub some dependencies using the Typings (= interfaces). I don't want to use the ANY-type and neither to write an empty method for each interface method.



Im looking for a way to do something like that:



let dependency = stub(IDependency);
stub(dependency.b(), () => {console.log(Hello World)});
dependency.a(); // --> Compile, do nothing, no exception
dependency.b(); // --> Compile, print Hello World, no exception


The pain I have right now, is that I either use any and implement all methods which get called in my test case or I implement the interface and implement the full interface. That's too much useless code :(.



How can I generate an object that has an empty implementation for each method and is typed? I use Sinon for mocking purposes, but im open to use other libraries too.



PS: I know that Typescript erases the interfaces...but I still would like to solve that :).


More From » angularjs

 Answers
39

I think the short answer is that this is not possible in Typescript, as the language offers no compile-time or run-time reflection. It's not possible for a mock library to iterate the members of an interface.



See thread: https://github.com/Microsoft/TypeScript/issues/1549



This is unfortunate for TDD developers, in which mocking a dependency is a central part of the development workflow.



There are a number of techniques for quickly stubbing the methods, however, as described by the other answers. These options might do the job, with a little mental adjustment.



Edit: The Typescript Abstract Syntax Tree, AST, is a compile-time introspection - which could probably be used to generate mocks. However, I don't know if anyone has made a practical library.


[#62307] Monday, May 2, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ariel

Total Points: 523
Total Questions: 111
Total Answers: 100

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;