Mocking framework

Jonathan M Davis jmdavisProg at gmx.com
Mon Mar 7 10:05:22 PST 2011


On Monday, March 07, 2011 01:46:36 simendsjo wrote:
> Are there any mocking frameworks that actually works for D2?
> And what is possible to mock, and what not?
> 
> My guess is only classes and interfaces can be mocked.
> And only non-final classes and non-final methods.
> Is this correct?

There's nothing official that does that sort of thing. There _might_ be a third 
party library out there (dsource.org would be the place to check, I guess), but 
most 3rd party D libraries (of any variety) are D1 rather than D2 (though there 
_are_ some which are D2). I'd be surprised if such a library exists, but maybe 
it does.

But if you're mocking up objects by deriving from them, then you're definitely 
not going to be able to override anything which is final, and if the whole class 
is final, then deriving from it will be impossible too. However, it _is_ very 
common to use structs instead of classes unless you actually need polymorphism, 
so it's quite common, and you can't override structs at all (though you could 
wrap them, and a lot of templated code would work with a wrapped struct as well 
as the original - but that depends on the template), so I'm not sure how much 
mileage the you'd get out of mock objects in D in general. Then again, I've 
never used a library for mock objects and have used hand-written ones rather 
rarely anyway, so I don't know that much about what you'd want to use them for.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list