NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

How to mock methods that take Span<T>?

Open asaf92 opened this issue 4 years ago • 3 comments

Question Is it possible to mock methods that take Span<T> and capture the arguments? Using Arg.Do, I'm getting an error that:

The type 'Span<...>' may not be used as a type argument

Example:

public interface IFoo
{
    public void Bar(Span<int> nums);
    public void Bar(IList<int> nums);
}

[Test]
public void MockDemo()
{
    var sum = 0;
    var mock = Substitute.For<IFoo>();
    mock.Bar(Arg.Do<IList<int>>(x => sum += x[0])); // This works
    // mock.Bar(Arg.Do<Span<int>>(x => sum += x[0])); // Error CS0306
}

asaf92 avatar Sep 17 '21 14:09 asaf92

I think we have to add support for Spans to get this working. So I tag this a feature request

304NotModified avatar Apr 29 '24 12:04 304NotModified

It has been 4 years. Any updates regarding this feature?

I'm facing a similar issue where I need a Span<byte> in Arg.Is() call.

artkolisnyk avatar Sep 11 '25 16:09 artkolisnyk

Unfortunately not. A PR is more than welcome

304NotModified avatar Sep 12 '25 12:09 304NotModified