You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The equality check in Interceptor.ExpressionKey (line 171) is wrong. The check must be conjunction (&), but not disjunction(|), because then the values don't matter and only the fixedString is considered, but not the values.
Also, the GetHashCode() must be calculated in a way the value position matters. Currently, it is val1.GetHashCode() ^ val2.GetHashCode() ^ ... ^ valN.GetHashCode(), but it should be:
( ... ((val1.GetHashCode() * <prime_num>) ^ val2.GetHashCode() * <prime_num>) ^ ... ^ valN.GetHashCode())
The fixes of these issues are in pull proposal - #134
So, invocations:
aMock.Setup(m => m.Foo(i1, i2));
aMock.Setup(m => m.Foo(i2, i1));
have the same expression key. As result the second overwrites the first and this is wrong.
Here is a test for it:
[Test]
public void MultipleSetupTest()
{
Mock aMock = new Mock(MockBehavior.Strict);
Item i1 = new Item { Id = 1 };
Item i2 = new Item { Id = 2 };
aMock.Setup(m => m.Foo(i1, i2));
aMock.Setup(m => m.Foo(i2, i1));
aMock.Object.Foo(i1, i2);
aMock.Object.Foo(i2, i1);
}
public interface IA
{
void Foo(Item x, Item y);
}
public class Item
{
public int Id { get; set; }
}
The issue can be worked around in one of the following ways:
Override ToString() of "Item", so it returns value dependent on the class fields ("Id").
Change the setup be:
aMock.Setup(m => m.Foo(It.Is(x => x.Id == i1.Id), It.Is(x => x.Id == i2.Id)));
aMock.Setup(m => m.Foo(It.Is(x => x.Id == i2.Id), It.Is(x => x.Id == i1.Id)));
Here the idea is again that the value is int and its ToString() is overriden.
In both cases the value is written in the "fixedString" of ExpressionKey and that's why it works.
The text was updated successfully, but these errors were encountered:
The equality check in Interceptor.ExpressionKey (line 171) is wrong. The check must be conjunction (&), but not disjunction(|), because then the values don't matter and only the fixedString is considered, but not the values.
Also, the GetHashCode() must be calculated in a way the value position matters. Currently, it is val1.GetHashCode() ^ val2.GetHashCode() ^ ... ^ valN.GetHashCode(), but it should be:
( ... ((val1.GetHashCode() * <prime_num>) ^ val2.GetHashCode() * <prime_num>) ^ ... ^ valN.GetHashCode())
The fixes of these issues are in pull proposal - #134
So, invocations:
aMock.Setup(m => m.Foo(i1, i2));
aMock.Setup(m => m.Foo(i2, i1));
have the same expression key. As result the second overwrites the first and this is wrong.
Here is a test for it:
[Test]
public void MultipleSetupTest()
{
Mock aMock = new Mock(MockBehavior.Strict);
}
public interface IA
{
void Foo(Item x, Item y);
}
public class Item
{
public int Id { get; set; }
}
The issue can be worked around in one of the following ways:
aMock.Setup(m => m.Foo(It.Is(x => x.Id == i1.Id), It.Is(x => x.Id == i2.Id)));
aMock.Setup(m => m.Foo(It.Is(x => x.Id == i2.Id), It.Is(x => x.Id == i1.Id)));
Here the idea is again that the value is int and its ToString() is overriden.
In both cases the value is written in the "fixedString" of ExpressionKey and that's why it works.
The text was updated successfully, but these errors were encountered: