soorajmamar / moq

Automatically exported from code.google.com/p/moq
Other
1 stars 0 forks source link

Setup of overloaded method fails under specific conditions #347

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

I've included a full set of test cases below

public interface InterfaceWithOverloadedMethods {
    void Method(Foo foo);
    void Method(Bar bar);
}

public class Foo {}

public class Foo2 : Foo {
    public override int GetHashCode() {
        // return base.GetHashCode();
        return 1;
    }
}

public class Bar {}

public class Bar2 : Bar {
    public override int GetHashCode() {
        // return base.GetHashCode();
        return 2;
    }
}

[Test]
public void FailingTest() {
    // Arrange
    var interfaceMock = new Mock<InterfaceWithOverloadedMethods>(MockBehavior.Strict);
    var fooMock = new Mock<Foo2>();
    var barMock = new Mock<Bar2>();

    interfaceMock.Setup(m => m.Method(fooMock.Object));
    interfaceMock.Setup(m => m.Method(barMock.Object));

    // Act
    interfaceMock.Object.Method(fooMock.Object);
    interfaceMock.Object.Method(barMock.Object);

    interfaceMock.Verify(x => x.Method(fooMock.Object), Times.Once());
    interfaceMock.Verify(x => x.Method(barMock.Object), Times.Once());
}

[Test]
public void PassingTest1() {
    // Arrange
    var interfaceMock = new Mock<InterfaceWithOverloadedMethods>(MockBehavior.Strict);
    var foo = new Foo();
    var bar = new Bar();

    interfaceMock.Setup(m => m.Method(foo));
    interfaceMock.Setup(m => m.Method(bar));

    // Act
    interfaceMock.Object.Method(foo);
    interfaceMock.Object.Method(bar);

    interfaceMock.Verify(x => x.Method(foo), Times.Once());
    interfaceMock.Verify(x => x.Method(bar), Times.Once());
}

[Test]
public void PassingTest2() {
    // Arrange
    var interfaceMock = new Mock<InterfaceWithOverloadedMethods>(MockBehavior.Strict);
    var fooMock = new Mock<Foo>();
    var barMock = new Mock<Bar>();

    interfaceMock.Setup(m => m.Method(fooMock.Object));
    interfaceMock.Setup(m => m.Method(barMock.Object));

    // Act
    interfaceMock.Object.Method(fooMock.Object);
    interfaceMock.Object.Method(barMock.Object);

    interfaceMock.Verify(x => x.Method(fooMock.Object), Times.Once());
    interfaceMock.Verify(x => x.Method(barMock.Object), Times.Once());
}

What is the expected output? What do you see instead?
I would expect the FailingTest() method to have passed, because of the 
different types which are being mocked.

What version of the product are you using? On what operating system?
This has been tested using Moq 4.0.10818.0 on Windows 7 Professional x64.

Please provide any additional information below.
A workaround to this issue was created by utilizing It.Is<Foo2> and It.Is<Bar2> 
in the Setup calls.

Original issue reported on code.google.com by arootb...@gmail.com on 10 Jul 2012 at 10:36

GoogleCodeExporter commented 8 years ago
Why are you overriding gethashcode? When you do that you also must override 
Equals, just like the compiler tells you.

Does that fix the issue?

Original comment by dan...@cazzulino.com on 10 Jul 2012 at 10:49

GoogleCodeExporter commented 8 years ago
No, adding an Equals override does not fix the issue.  The override of
GetHashCode is the only requirement to expose this issue, so that is the
only thing I included in the test code.

This test code is based on some internal code that I was creating tests for
when I stumbled across this issue.  That code contains overrides of Equals
and GetHashCode.

Original comment by arootb...@gmail.com on 10 Jul 2012 at 10:56