Skip to content
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

Wrong equality check in Interceptor.ExpressionKey #135

Closed
kostadinmarinov opened this issue Oct 8, 2014 · 1 comment
Closed

Wrong equality check in Interceptor.ExpressionKey #135

kostadinmarinov opened this issue Oct 8, 2014 · 1 comment

Comments

@kostadinmarinov
Copy link
Contributor

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:

  1. Override ToString() of "Item", so it returns value dependent on the class fields ("Id").
  2. 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.

@stakx
Copy link
Contributor

stakx commented Jun 5, 2017

Related: #252 (closed)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants