dmocks version 0.1

Christopher Wright dhasenan at gmail.com
Wed Nov 7 15:51:07 PST 2007


For anyone who doesn't know what mock objects are and wants to, there's 
Wikipedia: http://en.wikipedia.org/wiki/Mock_object

In brief, a mock object allows you to test how one class interacts with 
another, and test the functionality of each class independently of the 
rest. It does so by acting like an object that the class you are testing 
uses, but recording and checking which methods are called and returning 
values according to what the programmer explicitly told it to.

Christopher Wright wrote:
> Hello all.
> 
> For the Test-Driven Development people out there, and all the fans of 
> unittesting, I'm releasing a mock objects framework for D2, dmocks. It 
> can mock classes and interfaces, even if they are templated; it cannot 
> mock structs due to inheritance issues, and it can only mock virtual 
> methods (no templated methods, sorry; not my fault).
> 
> Download:
> http://damask-mud.googlecode.com/files/dmocks.zip
> 
> Subversion:
> svn checkout http://damask-mud.googlecode.com/svn/trunk/mocks/
> 
> Note that these are temporary locations, and I hope to move the project 
> to dsource shortly.
> 
> Examples:
> class Bar {
>    int doSomeCalculations (ubyte[] data) { ... }
> }
> class Foo {
>    private Bar bar;
>    this (Bar bar) { this.bar = bar; }
> 
>    int calculateStuff (ubyte[] data) {
>       int i = bar.doSomeCalculations (data);
>       ...
>       return i;
>    }
> 
>    unittest {
>       Mocker m = new Mocker();
>       auto bar = m.Mock!(Bar);
>       ubyte[] data = null;
> 
>       bar.doSomeCalculations(data);
>       m.LastCall().Return(5);
>       // or m.Expect(bar.doSomeCalculations(data)).Return(5);
> 
>       m.Replay();
>       Foo target = new Foo(bar);
>       target.calculateStuff(data);
> 
>       m.Verify();
>    }
> }
> 
> Address any complaints you have to dhasenan -at- gmail -dot- com. If 
> anyone has issues with style, I can offer both CamelCase and 
> abridgedCamelCase; if enough people request it, I'll offer a free 
> function interface if I can find a sane way of doing it. Complaints 
> about functionality will be addressed first.
> 
> 
> The framework in its current state represents about three days' work, 
> which is more a sign of the ease of using D than any special abilities I 
> have.
> 
> Dmocks is brought to you by __traits and string mixins. We therefore 
> regret to inform you that it is only available for D version 2.



More information about the Digitalmars-d-announce mailing list