-
-
Notifications
You must be signed in to change notification settings - Fork 811
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
Parameter types are ignored when matching an invoked generic method against setups #903
Comments
Here are the test cases to be added to the regression test suite.public class Issue903
{
public interface IX
{
void Method<T>(bool arg);
void Method<T>(int arg);
}
private readonly Mock<IX> mock;
public Issue903()
{
this.mock = new Mock<IX>();
}
[Fact]
public void Bool_method_was_setup_first__when_bool_method_invoked__bool_method_setup_should_be_matched()
{
var boolMethodInvoked = false;
this.SetupBoolMethod(() => boolMethodInvoked = true);
this.SetupIntMethod(() => throw new Exception("Wrong method called."));
this.InvokeBoolMethod();
Assert.True(boolMethodInvoked);
}
[Fact]
public void Bool_method_was_setup_last__when_bool_method_invoked__bool_method_setup_should_be_matched()
{
var boolMethodInvoked = false;
this.SetupIntMethod(() => throw new Exception("Wrong method called."));
this.SetupBoolMethod(() => boolMethodInvoked = true);
this.InvokeBoolMethod();
Assert.True(boolMethodInvoked);
}
[Fact]
public void Int_method_was_setup_last__when_int_method_invoked__int_method_setup_should_be_matched()
{
bool intMethodInvoked = false;
this.SetupBoolMethod(() => throw new Exception("Wrong method called."));
this.SetupIntMethod(() => intMethodInvoked = true);
this.InvokeIntMethod();
Assert.True(intMethodInvoked);
}
[Fact]
public void Int_method_was_setup_first__when_int_method_invoked__int_method_setup_should_be_matched()
{
bool intMethodInvoked = false;
this.SetupIntMethod(() => intMethodInvoked = true);
this.SetupBoolMethod(() => throw new Exception("Wrong method called."));
this.InvokeIntMethod();
Assert.True(intMethodInvoked);
}
private void InvokeBoolMethod()
{
this.mock.Object.Method<bool>(default(bool));
}
private void InvokeIntMethod()
{
this.mock.Object.Method<int>(default(int));
}
private void SetupBoolMethod(Action callback)
{
mock.Setup(m => m.Method<object>((bool)It.IsAny<object>())).Callback(callback);
}
private void SetupIntMethod(Action callback)
{
this.mock.Setup(m => m.Method<object>((int)It.IsAny<object>())).Callback(callback);
}
} |
stakx
added a commit
to stakx/moq
that referenced
this issue
Aug 24, 2019
ishimko
pushed a commit
to ishimko/moq4
that referenced
this issue
Sep 2, 2019
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Code to reproduce:
Expected outcome:
The test should succeed.
Actual outcome:
The test throws the
"Wrong setup matched."
exception.Further information:
While the concrete generic arguments in the above test are irrelevant (since Moq matches generic arguments by assignment compatibility), the presence of generic type parameters are central to this issue as they disable any parameter type checks that would otherwise happen. Note the
else
in the following:https://github.com/moq/moq4/blob/3868424bac43b2c08342b363eb6c511fdbc982ea/src/Moq/InvocationShape.cs#L113-L126
As a side note, setup order also matters for this test.
The text was updated successfully, but these errors were encountered: