[Unit tests] Mocking D objects
Simen Kjærås
simen.kjaras at gmail.com
Wed Aug 22 09:08:38 UTC 2018
On Wednesday, 22 August 2018 at 08:33:36 UTC, Andrey wrote:
> Hello,
> I know that D has build-in unit tests. If so, what mechanism D
> provides for mocking objects?
> For example:
>>struct WebParser
>>{
>> // ...
>>
>> int download(string path)
>> {
>> SomeHttpClient client(path);
>> auto result = client.request(path, 10, "Qwerty");
>>
>> // ...
>>
>> return result.getSomething();
>> }
>>}
>
> Here I want to replace struct/class SomeHttpClient from
> 3d-party library with my own test implementation. Something
> like this maybe:
> unittest
> {
> SomeMagicMockMechanism!(SomeHttpClient, MyMockedClient);
>
> WebParser parser;
> auto value = parser.download("www.example.com"); // uses
> MyMockedClient.request
> assert(value == 10);
>
> }
The language itself does not offer mocking capabilities. However,
there are excellent libraries in Dub:
https://code.dlang.org/packages/unit-threaded
https://code.dlang.org/packages/dmocks
--
Simen
More information about the Digitalmars-d-learn
mailing list