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
False positive "nested constraint" exception when using an argument constraint of the wrong type #1230
Comments
It works with 4.0 |
Thanks for reporting this @progala2. Not sure what's going on. It's a very basic use case, which should be covered extensively by our tests, so I'm really surprised it fails. I'll look into it ASAP. |
Nope. It works partially with 4.0 - it is registered without errors but doesn't return anything mailsToSend.AddRange(await _emailFactory.GetEmailMessages(game.Id, (EmailTemplateType)mailReminder.EmailTemplate.Id)); Yes it is a very basic case. I tried to clean the solution, I restarted VS... nothing helps, so I post it here :/ |
There's a problem in your code: it should be I think this is what causing the error, since I can't reproduce the problem when I change the constraint type. However, the exception is clearly incorrect, we definitely have a bug. |
yes, it works now. Jeez, I hate this type of "typos" 😖 |
Not saying this is it, but |
Bah! Github app didn't refresh the comments. I thought I was so smart. Glad the problem is found. Definitely a big. And @progala2, do consider picking up the analyzer. It can help in surprising ways. |
Installed 🗡 |
Ha, thank you. And I'm not just pushing it because the diagnostic in question the first one I put any significant effort into... 😉 |
The problem appears to be here. In this case, there is also an implicit conversion, but it's not a boxing conversion, so we don't eliminate it, and the constraint is seen as nested in the conversion. The problem is that there's no way to distinguish between explicit and implicit conversions with the Expression API... I think we should always eliminate the conversion, even if it's not a boxing conversion, but I'm a bit worried about unwanted side effects. |
@thomaslevesque, this still may not be fixed: [Scenario]
public static void ThatArgumentConstraintForDifferentValueTypeWithNonNullArgument(
IHaveANullableParameter subject,
int result)
{
"Given a fake with a method that accepts a nullable value type parameter"
.x(() => subject = A.Fake<IHaveANullableParameter>());
"And a call configured for a non-nullable argument of a different type"
.x(() => A.CallTo(() => subject.Bar(A<byte>._)).Returns(42));
"When I make a call to this method with a non-null argument"
.x(() => result = subject.Bar(7));
"Then it matches the configured call"
.x(() => result.Should().Be(42));
} fails, with a complaint about a nested constraint. The nullable parameter is an |
Yeah, this seems to fix that portion, at least: private static Expression GetExpressionWithoutConversion(Expression expression)
{
while (expression is UnaryExpression conversion && conversion.NodeType == ExpressionType.Convert)
{
expression = conversion.Operand;
}
return expression;
} Of course, this just means we consider the constraint not to be nested. It still does the wrong thing until #1237 is fixed. |
Pretty much. I can work on that later, if you like.
Oh. So have I. It was unowned, so I self-assigned and started working on it last night. |
This change has been released as part of FakeItEasy 4.1.1. Thanks for the report @progala2! Look for your name in the release notes. 🏆 |
Original title: Weird issue with nested constraints and simple types
EmailTemplateType is an enum. When I change it to int it is still wrong, so it is not an issue.
FakeIt version 4.1.
It throws the following exception
The text was updated successfully, but these errors were encountered: