Coder Social home page Coder Social logo

dmocks-revived's People

Contributors

linkrope avatar o3o avatar qaston avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

funkwerk

dmocks-revived's Issues

Variadic function cannot be mocked

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, )

How to handle certain contracts?

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

std.metastrings removed in 2.066

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.

@property cannot be mocked

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.)

Question: Missing example for Struct

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;
}

dmocks.dynamic(19): Assertion failure for returns(null)

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).

More verbose error message

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.

Breaking API change.

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.

How to handle multiple calls with different returns?

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

Allow for custom comparison of arguments

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.

DMocks-revived not in dub registry

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.