dimanche 21 octobre 2018

How can I test private methods with private return types?

I'm using PrivateObject to test private methods in one of my classes (please, no lectures on not testing private methods. I have chosen to do it.) You can then use Assert.AreEqual() to test the return value of your private method... if the return value is a basic built-in type. But what if my private method is returning a value which itself is a private struct?

[TestMethod]
public void TestPrivateMethod1() {
    using (Interactors.Interactor interactor = new Interactors.Interactor()) {
        var priv = new PrivateObject(interactor);
        var privateType = priv.Invoke("myPrivateMethod", "foo");
        Assert.AreEqual(privateType.SomeValue, 12);
    }
}

In this instance the private struct is defined inside the Interactors.Interactor class, and is returned into the variable privateType. But since the test method doesn't know about it, I obviously can't compile the code with privateType.SomeValue in it. How can I test that private return type's values?

Aucun commentaire:

Enregistrer un commentaire