vendredi 16 novembre 2018

Assert the execution order of private methods

I want to test that a method Foo, that calls three private methods boo1, boo2, boo3 is indeed calling them in this exact order (this is a simplification of the real scenario).

To test the sequence I am planning on using InSequence method of the Mock object.

To test the private method i am planning on using the PrivateObject class.

I have no idea how to combine those two; here what I've tried so far, but it doesn't seem to work!

public class TestedClass
{
    private void _boo1(object someParam)
    {
        //some logic
    }
    private void _boo2(object someParam)
    {
        //some logic
    }
    private void _boo3(object someParam)
    {
        //some logic
    }
    public void Foo(object someParam)
    {
        _boo1(someParam);
        _boo2(someParam);
        _boo3(someParam);
    }
}

And here the test method that I've created so far:

[TestMethod]
public void TestSequence()
{
        var sequence = new MockSequence();
        var mockedObject = new Mock<TestedClass>();
        PrivateObject obj = new PrivateObject(mockedObject);
        mockedObject.Object.Foo(It.IsAny<Object>());

        mockedObject.InSequence(sequence).Setup(x => obj.Invoke("_boo1", BindingFlags.Default, It.IsAny<Object>()));
        mockedObject.InSequence(sequence).Setup(x => obj.Invoke("_boo2", BindingFlags.Default, It.IsAny<Object>()));
        mockedObject.InSequence(sequence).Setup(x => obj.Invoke("_boo3", BindingFlags.Default, It.IsAny<Object>()));
}

Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire