atilaneves / unit-threaded

Advanced unit test framework for D
BSD 3-Clause "New" or "Revised" License
122 stars 38 forks source link

How do I mock a std.socket.Socket? #280

Closed andrewlalis closed 1 year ago

andrewlalis commented 1 year ago

Given the following code:

unittest {
    import std.socket;
    import unit_threaded;
    auto m = mock!Socket;
    assert(true);
}

I receive the following error output:

../../.dub/packages/unit-threaded-2.1.2/unit-threaded/subpackages/mocks/source/unit_threaded/mock.d-mixin-221(474,35): Error: function `@trusted long unit_threaded.mock.Mock!(Socket).Mock.MockAbstract.receiveFrom(void[] arg0, SocketFlags arg1, Address arg2)` does not override any function, did you mean to override `@trusted long std.socket.Socket.receiveFrom(void[] buf, std.socket.SocketFlags flags, ref std.socket.Address from)`?
../../.dub/packages/unit-threaded-2.1.2/unit-threaded/subpackages/mocks/source/unit_threaded/mock.d-mixin-221(489,35): Error: function `@safe long unit_threaded.mock.Mock!(Socket).Mock.MockAbstract.receiveFrom(void[] arg0, Address arg1)` does not override any function, did you mean to override `@trusted long std.socket.Socket.receiveFrom(void[] buf, std.socket.SocketFlags flags, ref std.socket.Address from)`?
../../.dub/packages/unit-threaded-2.1.2/unit-threaded/subpackages/mocks/source/unit_threaded/mock.d-mixin-221(549,33): Error: function `@trusted int unit_threaded.mock.Mock!(Socket).Mock.MockAbstract.getOption(SocketOptionLevel arg0, SocketOption arg1, int arg2)` does not override any function, did you mean to override `@trusted int std.socket.Socket.getOption(std.socket.SocketOptionLevel level, std.socket.SocketOption option, void[] result)`?
../../.dub/packages/unit-threaded-2.1.2/unit-threaded/subpackages/mocks/source/unit_threaded/mock.d-mixin-221(564,33): Error: function `@trusted int unit_threaded.mock.Mock!(Socket).Mock.MockAbstract.getOption(SocketOptionLevel arg0, SocketOption arg1, Linger arg2)` does not override any function, did you mean to override `@trusted int std.socket.Socket.getOption(std.socket.SocketOptionLevel level, std.socket.SocketOption option, void[] result)`?
../../.dub/packages/unit-threaded-2.1.2/unit-threaded/subpackages/mocks/source/unit_threaded/mock.d-mixin-221(577,33): Error: function `@trusted void unit_threaded.mock.Mock!(Socket).Mock.MockAbstract.getOption(SocketOptionLevel arg0, SocketOption arg1, Duration arg2)` does not override any function, did you mean to override `@trusted int std.socket.Socket.getOption(std.socket.SocketOptionLevel level, std.socket.SocketOption option, void[] result)`?
../../.dub/packages/unit-threaded-2.1.2/unit-threaded/subpackages/mocks/source/unit_threaded/mock.d(280,12): Error: template instance `unit_threaded.mock.Mock!(Socket)` error instantiating
source/handy_httpd/handlers/path_delegating_handler.d(100,23):        instantiated from here: `mock!(Socket)`
/usr/bin/dmd failed with exit code 1.

I'm not entirely sure what's going on here, and I'd appreciate any help in interpreting this error.

atilaneves commented 1 year ago

There were multiple bugs preventing this from compiling, which this PR fixes.

Unfortunately, if I try mocking std.socket.Socket after that, I get a memory violation at runtime. I don't have time to look into this though.