-
-
Notifications
You must be signed in to change notification settings - Fork 798
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
SetupSet problems with It.IsAny<> and indexer properties #218
Comments
I am experiencing the exact same issue. It would be nice when MockBehaviour.Strict and It.IsAny would work, fortunately in most of those cases omitting the mock behaviour does the trick. Is there any progress or are there any plans on this issue? |
I was looking at this a few days ago. IIRC there was a good reason why this doesn't work ATM, indexers and expression trees aren't too good friends and there's even a disabled unit test. I'll take a closer look in the next few days. |
PS: Feel free to do the same. If you find a solution you're welcome to send a PR! |
@misulo and @loedeman, here's a brief analysis of why this doesn't work: C# currently cannot convert a lambda involving an assignment to an expression tree. That is, the following won't compile: Expression<Action<string>> expr = str => str = null;
// ^^^^^^^^^^
// CS0832: An expression tree may not contain an assignment operator How then does Moq make it possible for you to pass such a lambda to As far as I can see, there are only a few options how to get this to work:
Or, we simply don't fix this at all. Any thoughts? |
Hi @stakx , thanks for your research and thorough explanation. Although it is not very comfortable not being able to fully test setters, just providing the actual value works well enough most of the time. Some edge cases (where you are not able to predict the setter value beforehand) would make the first option you are suggesting very useful. I agree that options 2 and 3 are not reasonable. Given the huge amount of work and the relatively small amount of edge cases (in my situation), I could simply live with option 4 as well for now ;). Cheers, Bert |
I might have been wrong with my previous assessment of option (2):
It turns out that decompilation might not be so much work after all: I wrote a small (approx. 150 LoC) prototype earlier today that could decompile the kinds of setup lambdas you'd typically see with Moq. Here's a small proof of concept library that I worked on today (functional, but far from being feature-complete). If we could base Moq on IL decompilation instead of the present mechanism ( /cc @kzu |
Reminder: When this issue is fixed, remember to restore these two unit tests in
|
Closing this, this problem will be tracked in #414 (together with a couple other related issues). |
Hi all
The topic is already discussed on Stack Overflow: http://stackoverflow.com/questions/29697406/moq-an-indexed-property-and-use-the-index-value-in-the-return-callback
I ran into it also and then looking if an issue is posted, couldn't find any. So thought I'd post one.
In a nutshell:
As a matter of fact, forget the Callback. Turn MockBehaviour to Strict, and try to mock the indexer Setter with a
It.IsAny<>
. It will not work.So right now the only workaround is not using the Strict behavior.
Same works without problems with SetupGet.
regards,
Misulo.
The text was updated successfully, but these errors were encountered: