© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
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
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);
public unsafe int GetPlatformIDs(uint num_entries, nint* platforms, out uint num_platforms);
(instead of
out uint num_platforms
out uint num_platforms
I could also use
uint* num_platforms
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);
            // ....
        }
        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
System.Linq.Expressions.Expression
so I always ran into the same errors)

Any idea how to do that?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Mocking derived class with abstract base class
C#CC# / help
4y ago
❔ Mocking EF Core extensions methods.
C#CC# / help
3y ago
Unit test help
C#CC# / help
17mo ago
Unit test error
C#CC# / help
2y ago