A technique to mock "static interfaces" (e.g. isInputRange)

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Thu May 26 13:03:50 PDT 2016


On 2016-05-25 23:38, Atila Neves wrote:

> Interesting? Crazy? Worth adding to unit-threaded? Phobos (after much
> cleaning up)?

Interesting, yes. Crazy, not enough :). Here's an example of replacing 
functions and replacing methods on individual objects [1].

I'm trying to do something like you can do in Ruby where there are 
mocking frameworks allowing you to replace any method with a new 
implementation, both on a class and object level. Not sure how bad it 
actually is to do something like this :)

One advantage of this technique is that the code does not need to be 
adapted for testing. For example, no need to make a function into a 
template just to be able to pass in a mock object/struct. Or if a 
function you would like to test writes to a file as a side effect for 
some reason. Just replace the "write" function with a dummy function 
that does nothing to avoid creating the file when running the test.

[1] https://dpaste.dzfl.pl/bfd933702ed6

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list