question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to work with ReadOnlySpan

See original GitHub issue

Hi

I have tried searching the documentation and the issues, but I did not find anything other than what seems to be “internal” implementation discussions.

I have an interface that defines a method with a ReadOnlySpan<char> argument. My current predicament is that I need to define a return value from that method for my test.

Using A.CallTo(() => thing.Method(A<ReadOnlySpan<char>>._)).Returns("something") just states that a ReadOnlySpan cannot be used as an argument and if I run the test anyway, I get a NotSupportedException.

System.NotSupportedException
Specified method is not supported.
   at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException()
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at FakeItEasy.ExpressionExtensions.Evaluate(Expression expression) in C:\projects\fakeiteasy\src\FakeItEasy\ExpressionExtensions.cs:line 28
   at FakeItEasy.Expressions.ExpressionArgumentConstraintFactory.<>c__DisplayClass13_0.<GetArgumentConstraintFromExpression>b__0() in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\ExpressionArgumentConstraintFactory.cs:line 167
   at FakeItEasy.Core.ArgumentConstraintTrap.TrapConstraintOrCreate(Action actionThatProducesConstraint, Func`1 defaultConstraintFactory) in C:\projects\fakeiteasy\src\FakeItEasy\Core\ArgumentConstraintTrap.cs:line 32
   at FakeItEasy.Expressions.ExpressionArgumentConstraintFactory.GetArgumentConstraintFromExpression(Expression expression, Type parameterType, Object& value) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\ExpressionArgumentConstraintFactory.cs:line 166
   at FakeItEasy.Expressions.ExpressionArgumentConstraintFactory.GetArgumentConstraint(ParsedArgumentExpression argument) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\ExpressionArgumentConstraintFactory.cs:line 30
   at FakeItEasy.Expressions.ExpressionCallMatcher..ctor(ParsedCallExpression parsedExpression, ExpressionArgumentConstraintFactory constraintFactory, MethodInfoManager methodInfoManager) in C:\projects\fakeiteasy\src\FakeItEasy\Expressions\ExpressionCallMatcher.cs:line 34
   at FakeItEasy.RootModule.<>c__DisplayClass1_0.<RegisterDependencies>g__ExpressionCallRuleFactory|3(ParsedCallExpression callSpecification) in C:\projects\fakeiteasy\src\FakeItEasy\RootModule.cs:line 112
   at FakeItEasy.Configuration.FakeConfigurationManager.CallTo[T](Expression`1 callSpecification) in C:\projects\fakeiteasy\src\FakeItEasy\Configuration\FakeConfigurationManager.cs:line 48
   at FakeItEasy.A.CallTo[T](Expression`1 callSpecification) in C:\projects\fakeiteasy\src\FakeItEasy\A.cs:line 161
   at test_unit.MyThing.SomethingTests..ctor()

I have tried matching with A<string>._ and A<char[]>._ as well, but either I get other warnings or I am back to the NotSupportedException

In my case I am dealing with large strings, so I would very much like to use ReadOnlySpan, but it is also critical code that I definitely want to verify does what it is supposed to do.

Any ideas?

If it is not possible to do at all, then I would like to suggest adding something to the documentation about it.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pingvinencommented, Aug 2, 2021

if I run the test anyway

How? The code doesn’t compile, how can you run the test anyway?

It compiles with A<char[]>._. I have target=netcoreapp3.1 and sdk=5.x.

this is a limitation imposed by the compiler.

I was starting to fear that based on similar discussions going on in all the other mocking frameworks out there. If you ever run into a pattern that works, which allows code to be tested and optimized using these new-ish types, please put it in the documentation 😃

Btw… in my specific case I ended up implementing my interface in the test like so…

public class MockableThing : IThing
{
	public string Method(ReadOnlySpan<char> data)
	{
		return MockableMethod(data.ToString());
	}

	public virtual string MockableMethod(string data)
	{
		throw new NotImplementedException();
	}
}

With a setup like…

var thing = A.Fake<MockableThing>();

A.CallTo(() => thing.MockableMethod(A<string>._)).Returns("something");

This is not pretty and might not be feasible in all cases.

Thank you @thomaslevesque, for taking the time to work on FakeItEasy 😃

0reactions
thomaslevesquecommented, Aug 3, 2021

That seems like a good solution in your case! Glad you were able to resolve the issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# - All About Span: Exploring a New .NET Mainstay
Span<T> is a new value type at the heart of .NET. It enables the representation of contiguous regions of arbitrary memory, regardless of...
Read more >
How to Use Span in C# to Improve Application Performance
First, let's discuss how we can use ReadOnlySpan<> when operating over strings to get the performance benefits we are searching for.
Read more >
Writing High-Performance Code Using Span<T> and ...
A ReadOnlySpan<T> instance is often used to refer to array items or a chunk of an array. As opposed to arrays, ReadOnlySpan<T> instances...
Read more >
Improve C# code performance with Span<T>
Let's use Span<T> to obtain an array of uint from the string "163,496,691,1729" . ... The method uint.Parse(ReadOnlySpan<char>) shows that it's ...
Read more >
An Introduction to Writing High-Performance C# Using Span ...
Let's do the same with Span. For this we are going to use a version of Span<T> called ReadOnlySpan<T>. As the span is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found