Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking a method that takes an in parameter #1301

Closed
foxesknow opened this issue Nov 21, 2022 · 1 comment
Closed

Mocking a method that takes an in parameter #1301

foxesknow opened this issue Nov 21, 2022 · 1 comment

Comments

@foxesknow
Copy link

I have a method that takes a large readonly struct as an in parameter. I'd like to setup a mock so that I can match against a certain property on the struct.

Because it's passed as in I need to use It.Ref. However, there's no option to attach a predicate. For example I'd like to say:

myMock1.Setup(x => x.Visit(ref It.Ref<LargeStruct>.Is(r = r.UserID == 10))

The only option available is to match any instance of LargeStruct which isn't a narrow enough condition.

Is there an alternative way to handle this scenario?

@stakx
Copy link
Contributor

stakx commented Dec 11, 2022

Unfortunately, something like It.Ref<T>.Is(predicate) cannot currently be implemented, because the C# compilers do not allow ref returns in LINQ expression trees. (If you try to do so, you'll get an error CS8153: An expression tree lambda may not contain a call to a method, property, or indexer that returns by reference.) So as far as I can tell, what you're asking for is currently not possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants