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

Unexpected behaviour with MockExtensions.Reset #220

Closed
tonyhallett opened this issue Dec 2, 2015 · 3 comments · Fixed by #277
Closed

Unexpected behaviour with MockExtensions.Reset #220

tonyhallett opened this issue Dec 2, 2015 · 3 comments · Fixed by #277

Comments

@tonyhallett
Copy link
Contributor

The only test for Reset shows that for a loose mock set up with a return value, after Reset has been called, the return value defaults. Aside from the additional reset calls is this the only intended behaviour ? I ask as this is not what I expected from the documentation ( although I am not familiar with Testeroids if this is the sole purpose )

I expected that the mock would behave as if no setup had been done. e.g a strict mock will not throw when invoking a method that has been setup but will when reset has subsequently been called.

@cscorley
Copy link

cscorley commented Jun 23, 2016

I'm also experiencing this confusion with what this method is supposed to accomplish. I think it does exactly what it says it does, but perhaps the documentation around it is a bit misleading about what "mock state, including setups" means.

For example, all of these tests fail on the VerifyAll:

[TestClass]
public class MoqResetVerifyTests {
    [TestMethod]
    [TestCategory("Unit")]
    public void Loose()
    {
        var myMock = new Mock<IEnumerable<int>>(MockBehavior.Loose);
        myMock
            .Setup(a => a.ToString())
            .Returns("Hello");
        myMock.Reset();
        Assert.AreNotEqual("Hello", myMock.Object.ToString());
        myMock.VerifyAll();
    }

    [TestMethod]
    [TestCategory("Unit")]
    public void Strict()
    {
        var myMock = new Mock<IEnumerable<int>>(MockBehavior.Strict);
        myMock
            .Setup(a => a.ToString())
            .Returns("Hello");
        myMock.Reset();
        Assert.AreNotEqual("Hello", myMock.Object.ToString());
        myMock.VerifyAll();
    }

    [TestMethod]
    [TestCategory("Unit")]
    public void LooseNoCall()
    {
        var myMock = new Mock<IEnumerable<int>>(MockBehavior.Loose);
        myMock
            .Setup(a => a.ToString())
            .Returns("Hello");
        myMock.Reset();
        myMock.VerifyAll();
    }

    [TestMethod]
    [TestCategory("Unit")]
    public void StrictNoCall()
    {
        var myMock = new Mock<IEnumerable<int>>(MockBehavior.Strict);
        myMock
            .Setup(a => a.ToString())
            .Returns("Hello");
        myMock.Reset();
        myMock.VerifyAll();
    }
}

@kzu
Copy link
Contributor

kzu commented Jun 23, 2016

Could you post the failures too, for the record?

On Thu, Jun 23, 2016 at 3:27 PM Christopher Corley notifications@github.com
wrote:

I'm also experiencing this confusion with what this method is supposed to
accomplish. I think it does exactly what it says it does, but perhaps the
documentation around it is a bit misleading. For example, all of these
tests fail on the VerifyAll:

[TestClass]public class MoqResetVerifyTests {
[TestMethod]
[TestCategory("Unit")]
public void Loose()
{
var myMock = new Mock<IEnumerable>(MockBehavior.Loose);
myMock
.Setup(a => a.ToString())
.Returns("Hello");
myMock.Reset();
Assert.AreNotEqual("Hello", myMock.Object.ToString());
myMock.VerifyAll();
}

[TestMethod]
[TestCategory("Unit")]
public void Strict()
{
    var myMock = new Mock<IEnumerable<int>>(MockBehavior.Strict);
    myMock
        .Setup(a => a.ToString())
        .Returns("Hello");
    myMock.Reset();
    Assert.AreNotEqual("Hello", myMock.Object.ToString());
    myMock.VerifyAll();
}

[TestMethod]
[TestCategory("Unit")]
public void LooseNoCall()
{
    var myMock = new Mock<IEnumerable<int>>(MockBehavior.Loose);
    myMock
        .Setup(a => a.ToString())
        .Returns("Hello");
    myMock.Reset();
    myMock.VerifyAll();
}

[TestMethod]
[TestCategory("Unit")]
public void StrictNoCall()
{
    var myMock = new Mock<IEnumerable<int>>(MockBehavior.Strict);
    myMock
        .Setup(a => a.ToString())
        .Returns("Hello");
    myMock.Reset();
    myMock.VerifyAll();
}

}


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#220 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAKW663pMT4aeKWZM_4ToBA53E6kMlNlks5qOtAKgaJpZM4GtDCE
.

@cscorley
Copy link

cscorley commented Jun 23, 2016

Sure, sorry for leaving that out! This is with Moq 4.5.10, targeting .NET 4.5.2. The project is blank, with the exception of UnitTest1.cs, which contains previous class.

c:\Users\christopher.corley\Documents\Visual Studio 2015\Projects\UnitTestProject1\UnitTestProject1>vstest.console.exe bin\Debug\UnitTestProject1.dll
Microsoft (R) Test Execution Command Line Tool Version 14.0.25123.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
Failed   Loose
Error Message:
   Test method UnitTestProject1.MoqResetVerifyTests.Loose threw exception:
Moq.MockVerificationException: The following setups were not matched:
IEnumerable`1 a => a.ToString()

Stack Trace:
    at Moq.Mock.VerifyAll()
   at UnitTestProject1.MoqResetVerifyTests.Loose() in c:\users\christopher.corley\documents\visual studio 2015\Projects\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 20

Failed   Strict
Error Message:
   Test method UnitTestProject1.MoqResetVerifyTests.Strict threw exception:
Moq.MockVerificationException: The following setups were not matched:
IEnumerable`1 a => a.ToString()

Stack Trace:
    at Moq.Mock.VerifyAll()
   at UnitTestProject1.MoqResetVerifyTests.Strict() in c:\users\christopher.corley\documents\visual studio 2015\Projects\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 33

Failed   LooseNoCall
Error Message:
   Test method UnitTestProject1.MoqResetVerifyTests.LooseNoCall threw exception:
Moq.MockVerificationException: The following setups were not matched:
IEnumerable`1 a => a.ToString()

Stack Trace:
    at Moq.Mock.VerifyAll()
   at UnitTestProject1.MoqResetVerifyTests.LooseNoCall() in c:\users\christopher.corley\documents\visual studio 2015\Projects\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 45

Failed   StrictNoCall
Error Message:
   Test method UnitTestProject1.MoqResetVerifyTests.StrictNoCall threw exception:
Moq.MockVerificationException: The following setups were not matched:
IEnumerable`1 a => a.ToString()

Stack Trace:
    at Moq.Mock.VerifyAll()
   at UnitTestProject1.MoqResetVerifyTests.StrictNoCall() in c:\users\christopher.corley\documents\visual studio 2015\Projects\UnitTestProject1\UnitTestProject1\UnitTest1.cs:line 57


Total tests: 4. Passed: 0. Failed: 4. Skipped: 0.
Test Run Failed.
Test execution time: 2.5155 Seconds

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

Successfully merging a pull request may close this issue.

3 participants