qaston / dmocks-revived Goto Github PK
View Code? Open in Web Editor NEWA mocking framework for the D programming language
License: BSD 3-Clause "New" or "Revised" License
A mocking framework for the D programming language
License: BSD 3-Clause "New" or "Revised" License
I am still trying to mock InputStream
and OutputStream
from std.stream
:
interface Dependency
{
void foo(...);
}
unittest
{
auto mocker = new Mocker;
auto dependency = mocker.mock!Dependency;
}
does not compile:
dmocks/object_mock.d(30): Error: function dmocks.object_mock.Mocked!(Dependency).Mocked.foo does not override any function, did you mean to override 'test.Dependency.foo'?
dmocks/factory.d(15): Error: template instance dmocks.object_mock.Mocked!(Dependency) error instantiating
dmocks/mocks.d(106): instantiated from here: mock!(Dependency, )
interface Dependency
{
int foo()
out(result) { assert(result != int.init); }
}
unittest
{
auto mocker = new Mocker;
auto dependency = mocker.mock!Dependency;
mocker.expect(dependency.foo).returns(42);
mocker.replay;
assert(dependency.foo == 42);
}
fails in dmocks.mocks.ExpectationSetup dmocks.mocks.Mocker.expect
: Assertion failure
Instalation of dmocks-revived through d fails with:
Running dmd...
../../../.dub/packages/dmocks-revived-master/dmocks/method_mock.d(5): Error: module metastrings is in file 'std/metastrings.d' which cannot be read
import path[0] = ../../../.dub/packages/dmocks-revived-master
import path[1] = /opt/dmd-2.066/import
I believe this is because std.metastrings is removed from phobos in 2.066.
interface Dependency
{
@property int foo();
}
unittest
{
auto mocker = new Mocker;
auto dependency = mocker.mock!Dependency;
}
does not compile:
dmocks/object_mock.d(30): Error: template instance ReturnType!int does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func)
dmocks/object_mock.d(31): Error: template instance ParameterTypeTuple!int does not match template declaration ParameterTypeTuple(func...) if (func.length == 1 && isCallable!func)
dmocks/factory.d(15): Error: template instance dmocks.object_mock.Mocked!(Dependency) error instantiating
dmocks/mocks.d(106): instantiated from here: mock!(Dependency, )
(In the real code, I tried to mock the InputStream
and the OutputStream
from std.stream
.)
I can't work out how to mock structs. I want to use the cairoD library, and want to mock their main struct (Context), so I can add tests without actually having to draw anything.
I am able to get a MockedStruct!Context, but do not understand how to then pass it to the next function. Resulting in the following error: Error: function plotd.drawing.funcToTest (Context cnt) is not callable using argument types (MockedStruct!(Context))
void funcToTest(cairo.Context cnt)
{
cnt.fill();
}
unittest
{
import dmocks.mocks;
auto mocker = new Mocker();
auto axes_surface = new cairo.ImageSurface(
cairo.Format.CAIRO_FORMAT_ARGB32, 400, 400);
//auto axes_context = cairo.Context( axes_surface );
auto mock = mocker.mockStruct!(cairo.Context, cairo.ImageSurface )(
axes_surface );
mocker.expect(mock.fill()).repeat( 1 );
mocker.replay;
funcToTest(mock);
mocker.verify;
}
interface Dependency
{
string foo();
}
unittest
{
auto mocker = new Mocker;
auto dependency = mocker.mock!Dependency;
mocker.expect(dependency.foo).returns(null);
mocker.replay;
assert(dependency.foo is null);
}
A workaround would be returns(cast(string) null)
.
dmock is an excellent tool, but when things go wrong is difficult to understand where.
We can have more verbose error messages when an expectation does not occur?
For example compare NSubstitute (C#) message:
1)Layer1Test.SendAndWait_should_discard : NSubstitute.Exceptions.ReceivedCallsException :
Expected to receive a call matching:
DiscardBuffers()
Actually received no matching calls.
with this DMock message:
dmocks.util.ExpectationViolationException@dmocks/util.d(83): ExpectationViolationException: An unexpected call has occurred.
----------------
bin/protocols-test(void dmocks.object_mock.__T14mockMethodCallS41_D9protocols7channel8IChannel5writeMFAhZvVAyaa5_7772697465TC9protocols7channel8IChannelTC6dmocks11object_mock40__T6MockedTC9protocols7channel8IChannelZ6MockedTC6dmocks6caller6CallerTDFNbNfAhZvTAhZ.mockMethodCall(dmocks.object_mock.Mocked!(protocols.channel.IChannel).Mocked, dmocks.caller.Caller, nothrow @safe void delegate(ubyte[]), ref ubyte[]).setRope()+0x50) [0x491a10]
bin/protocols-test(void dmocks.object_mock.__T14mockMethodCallS41_D9protocols7channel8IChannel5writeMFAhZvVAyaa5_7772697465TC9protocols7channel8IChannelTC6dmocks11object_mock40__T6MockedTC9protocols7channel8IChannelZ6MockedTC6dmocks6caller6CallerTDFNbNfAhZvTAhZ.mockMethodCall(dmocks.object_mock.Mocked!(protocols.channel.IChannel).Mocked, dmocks.caller.Caller, nothrow @safe void delegate(ubyte[]), ref ubyte[])+0x81) [0x491981]
bin/protocols-test(void dmocks.object_mock.Mocked!(protocols.channel.IChannel).Mocked.write(ubyte[])+0xa0) [0x491420]
bin/protocols-test(ubyte[] protocols.commandProtocol.CommandProtocol.sendAndReceive(ubyte, ubyte[])+0x83) [0x494dab]
bin/protocols-test(void protocols.commandProtocol.CommandProtocol.__unittestL23_10()+0x1db) [0x494f93]
bin/protocols-test(void protocols.commandProtocol.__modtest()+0x9) [0x495059]
bin/protocols-test(int core.runtime.runModuleUnitTests().__foreachbody3(ref object.ModuleInfo*)+0x30) [0x4aa8e0]
.....
makefile:41: recipe for target 'test' failed
make: *** [test] Error 1
Thank you.
Regardless of the number of people using dmocks it's probably nice to warn about future breaking changes to the API.
Due to it's current way of handling errors dmocks has problems with mocking nothrow methods. Right now expectation exceptions are thrown while methods are replayed - this doesn't play well with nothrow. My plan is to make expectation validation happen on Mocker.verify (as it probably should be in the first place) - therefore for future compatibility you should call Mocker.verify when you expect expectation exceptions.
class Dependency
{
public int foo() { assert(0); }
}
unittest
{
auto mocker = new Mocker;
auto dependency = mocker.mock!Dependency;
mocker.ordered;
mocker.expect(dependency.foo).returns(1);
mocker.expect(dependency.foo).returns(2);
mocker.replay;
assert(dependency.foo == 1);
assert(dependency.foo == 2);
mocker.verify;
}
throws ExpectationViolationException: Dependency.foo() Expected: 1..1 Actual: 2
When a function to be mocked has an argument for which the comparison with ==
does not work, you have to switch to ignoreArgs
and to action
. In the action block you have to explicitly compare all simple arguments with ==
and the offending one with your own custom comparison.
One example of an offending argument is Nullable!T()
, where ==
throws.
It would be nice, when the comparison of arguments could be customized. Then you would provide a specialized equality predicate for a type like Nullable!JSONValue
(or even JSONValue
) and could use the mock framework as usual.
Since it's so easy to add packages to the registry and this project uses dub already, it would make it easy for users to use DMocks-revived if it were on it.
@QAston I saw you just updated the Readme. The maintained repository is acutally another one: https://github.com/funkwerk/dmocks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.