C#C
C#3y ago
cap5lut

❔ Unit Test | Mocking class with unsafe methods

Hi, I am having trouble writing unit tests, because I dont know how to mock/setup the underlying instance/its methods.

The class Silk.NET.OpenCL.CL (https://github.com/dotnet/Silk.NET/blob/main/src/OpenCL/Silk.NET.OpenCL/CL.gen.cs) does not implement an interface and the methods are unsafe and non virtual.
This would be one of the methods I need to mock:
public unsafe int GetPlatformIDs(uint num_entries, nint* platforms, out uint num_platforms);
(instead of out uint num_platforms I could also use uint* num_platforms, if that matters)

I tried using Moq, but I run into errors for the null pointer and count parameter:
        unsafe
        {
            var cl = new Mock<CL>();
            cl.Setup(instance => instance.GetPlatformIDs(0, null, out var count)).Returns(0);
            // ....
        }

CS1944: An expression tree may not contain an unsafe pointer operation
CS8198: An expression tree may not contain an out argument variable declaration
( I tried also other libraries like Pose, but they all use System.Linq.Expressions.Expression so I always ran into the same errors)

Any idea how to do that?
Was this page helpful?