Is there a way to work with ReadOnlySpan
See original GitHub issueHi
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:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
It compiles with
A<char[]>._
. I have target=netcoreapp3.1 and sdk=5.x.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…
With a setup like…
This is not pretty and might not be feasible in all cases.
Thank you @thomaslevesque, for taking the time to work on
FakeItEasy
😃That seems like a good solution in your case! Glad you were able to resolve the issue