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

Use of SetupSet 'forgets' Method Setup #432

Closed
TimothyHayes opened this issue Aug 9, 2017 · 2 comments · Fixed by #782
Closed

Use of SetupSet 'forgets' Method Setup #432

TimothyHayes opened this issue Aug 9, 2017 · 2 comments · Fixed by #782
Labels

Comments

@TimothyHayes
Copy link

TimothyHayes commented Aug 9, 2017

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?

@stakx
Copy link
Contributor

stakx commented Aug 18, 2017

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

@stakx stakx added the bug label Sep 21, 2017
@stakx
Copy link
Contributor

stakx commented Jul 13, 2018

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

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