Using the Setup Logging SetupSet Method

While I was experimenting to solve a different situation with Moq, I tried to use SetupSet to solve it. This revealed another potential problem.

When I use the SetupSet for the property along with the setting in the method, Moq seems to “forget” that the setting in the method is complete.

Here is a 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?");
    }
}

The following are unit tests:

public class PrancerTests
{
    [Fact]
    public void Antlers_NoSetup()
    {
        // Arrange

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

        // Act
        sut.Object.Antlers = true;

        // Assert
        sut.VerifySet(x => x.Antlers = true);
    }

    [Fact]
    public void Antlers_SetupProperty()
    {
        // Arrange

        // 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()); // nullify

        // Act
        sut.Object.Antlers = true;

        // Assert
        sut.VerifySet(x => x.Antlers = true);
    }

    [Fact]
    public void Antlers_SetupSet()
    {
        // Arrange

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

        // Act
        sut.Object.Antlers = true;

        // Assert
        sut.VerifySet(x => x.Antlers = true);
    }

}

the unit test, where I use SetupSet, reports an exception ("Why am I here?") thrown in the ExecuteMe () method, which proves that the ExecuteMe () method is executed even if it is installed (x => x. ExecuteMe ()) to prevent it. The remaining two unit tests pass (and, apparently, do not execute ExecuteMe ()).

Setup ExecuteMe(), . ( ) Setup SetupSet, .

, SetupSet Setup?

+4
1

, SetupSet Setup?

, Moq. Moq GitHub moq/moq4? ( , , SO.)

, . ( , GitHub, SO.) SetupSet:

sut.SetupSet(x => x.Antlers = true);

Moq LINQ (Expression<Action<TMock,…>> Expression<Func<TMock,…>>) . - " ", Moq , , , ( ), ( ).

- # ( , lambdas, ), Moq SetupSet ; Action<TMock>, , . Moq , . , , Moq " ", ( FluentMockContext). , " " .

, @Kritner :

SetupSet , /.

, mock, . , . CallBase = true, Moq ExecuteMe. , .

, CallBase " " , CallBase , Moq, ( - ), " " .

" " , . Moq, , ( .) (FluentMockContext), .

, Moq GitHub, .

+1

Source: https://habr.com/ru/post/1682501/


All Articles