moq icon indicating copy to clipboard operation
moq copied to clipboard

Use of SetupSet 'forgets' Method Setup

Open TimothyHayes opened this issue 9 years ago • 2 comments

This was posted to StackOverflow here. The person who responded, known as 'stakx' seems to be on the right track.

While I was experimenting to resolve a different situation with Moq, I attempted to use SetupSet to resolve. This uncovered another potential problem.

When I use SetupSet on a property along with a Setup on a Method, Moq seems to 'forget' that the Setup on the Method has been done.

Here is sample code, very simple:

public class Prancer
{

    public Prancer(bool pIsMale)
    {
        IsMale = pIsMale;
        ExecuteMe();
    }

    private bool _IsMale;
    public virtual bool IsMale
    {
        get { return this._IsMale; }
        private set { this._IsMale = value; }
    }

    private bool _Antlers;
    public virtual bool Antlers
    {
        get { return this._Antlers; }
        set
        {
            this._Antlers = value;
        }
    }

    public virtual void ExecuteMe()
    {
        throw new Exception("Why am I here?");
    }
}

Here are the unit tests:

public class PrancerTests
{
    [Fact]
    public void Antlers_NoSetup()
    {
        // Arrange
        int temp = 0;

        // create mock of class under test
        var sut = new Mock<Prancer>(true) { CallBase = true };
        sut.Setup(x => x.ExecuteMe()).Callback(() => temp = 1); // nullify

        // Act
        sut.Object.Antlers = true;

        // Assert
        sut.VerifySet(x => x.Antlers = true);
        Assert.Equal(1, temp);
    }

    [Fact]
    public void Antlers_SetupProperty()
    {
        // Arrange
        int temp = 0;

        // create mock of class under test
        var sut = new Mock<Prancer>(true) { CallBase = true };
        sut.SetupProperty(x => x.Antlers, false);
        sut.Setup(x => x.ExecuteMe()).Callback(() => temp = 2); // nullify

        // Act
        sut.Object.Antlers = true;

        // Assert
        sut.VerifySet(x => x.Antlers = true);
        Assert.Equal(2, temp);
    }

    [Fact]
    public void Antlers_SetupSet()
    {
        // Arrange
        int temp = 0;

        // create mock of class under test
        var sut = new Mock<Prancer>(true) { CallBase = true };
        sut.Setup(x => x.ExecuteMe()).Callback(() => temp = 3); // nullify
        sut.SetupSet(x => x.Antlers = true);

        // Act
        sut.Object.Antlers = true;

        // Assert
        sut.VerifySet(x => x.Antlers = true);
        Assert.Equal(3, temp);
    }
}

The unit test where I use SetupSet reports the Exception ("Why am I here?") thrown in method ExecuteMe() which proves that the method ExecuteMe() executed even when there was a Setup(x => x.ExecuteMe()) to prevent it. The other two unit tests pass (and apparently do not execute ExecuteMe()).

I even attempted to put a Callback on the Setup for ExecuteMe(), but same result. I also reversed the order (in code) of the Setup and SetupSet, to no avail.

Any thoughts why the SetupSet could have affected the method Setup?

TimothyHayes avatar Aug 09 '17 16:08 TimothyHayes

Thanks for reporting. I've added it to the list of FluentMockContext-related bugs (see above).

stakx avatar Aug 18 '17 08:08 stakx

Closing this, this problem will be tracked in #414 (together with a couple other related issues).

stakx avatar Jul 13 '18 20:07 stakx