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
Argument Constraints should support derived types as Type Arguments #326
Comments
Potential fix seems pretty easy: In the class MatchesConstraint (in DefaultAurgumentConstraintManager.cs), Add a type check (around line 74): if (argument is T)
return this.predicate.Invoke((T)argument);
return false; |
Thanks for rasing this, @FUR10N. (Note that I cleaned up the issue description using https://help.github.com/articles/github-flavored-markdown#fenced-code-blocks.) It may be neither expected or a bug. If anyone showed me this, I'd suggest they rewrite their predicate to take Your idea for a fix is a good one, but I have some concerns. But as you suggest, the error we get back:
leaves something to be desired. I like the idea of trapping the error and providing a more readable message for someone, but we may need to take a slightly different approach. I'm sure one could be devised, and it might even handle exceptions raised from within the predicate, for example. |
Using Also, in my sample code, if all of the calls were made with a parameter of the same type, then you'd be fine. Should it fail in that case too? It feels weird that it should only fail in some cases where multiple calls are made. |
Regarding it being an unusual operation, I think if you ignore the semantics, then yes it's unusual. But if you look at what you're trying to accomplish, then it makes sense to me to do something like that. Essentially what I want to ask with the verify statement is: Verify that I called this function with an argument that matches this type. Not, verify that all calls made to this function match this argument type. |
Ah, yes. I missed the bit where you had a passing call in the test, so even cleaning up the exception isn't quite what you need (still a good idea, I think, but that's another topic). It still feels a little weird to me to have the derived type in the So, working from your original proposal, it would be possible to examine the argument, and if it's null OR Hmm... if |
I don't think it's that awfully weird of a thing to do. So if the following method call is valid: t.SomeFunction(new SomeDerivedClass()); Then I'd expect a similar verify call to work: A.CallTo(() => t.SomeFunction(A<SomeDerivedClass>.That.Matches)) Rather than having to write a predicate that matches whatever base type the method might use. A.CallTo(() => t.SomeFunction(A<BaseClass>.That.Matches(i=> if (i is DerivedClass) return checkValues((DerivedClass)i);)) But I do agree, I didn't take into account all inputs in the change I proposed. That does need some work. I had actually used |
Actually, I think this should 'just work' in all cases: if (argument is T || argument == null) If Look at unbounded parameters section for null checking |
It looks promising. Are you interested in trying it out? The issue can be yours! |
Sure! It'll probably sometime Monday before I can look at this again, so don't wait up on this potentially crucial change. |
Thanks! |
Marking as taken by @FUR10N, putting in Working status, and slotting in Milestone 1.22.0. |
Renamed from "InvalidCastException in A.CallTo(...).MustHaveHappened()" to "Argument Constraints should support derived types as Type Arguments" |
👍 |
Fixes issue #326 - Argument Constraints should support derived types as Type Arguments
@FUR10N, thanks very much for your work on this issue. Look for your name in the release notes. 🏆 This issue has been fixed in release 1.22.0. https://www.nuget.org/packages/FakeItEasy/1.22.0 |
Steps to reproduce:
Is this expected, or a bug?
The code I used to test it:
And the output: System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.String'
The text was updated successfully, but these errors were encountered: