NSubstitute
NSubstitute copied to clipboard
How to mock methods that take Span<T>?
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
}
I think we have to add support for Spans to get this working. So I tag this a feature request
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.
Unfortunately not. A PR is more than welcome