Search code examples
c#unit-testingprivate-methods

How to do the Assert phase of a private method whose return type is Void?


I have a class with a private method

public class MyClass 
{
    private void SomeMethod(PrimaryAllocationDP packet)
    {
        ........................
        some code
        ........................
        packet.AllocatedAgency = AgencyAllocated;
    }
} 

Now by using MSUnit Testing framework, I have written so far

[TestMethod]
public void TestAllocatedAgency()
{ 

    var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here

    PrivateObject accessor = new PrivateObject(new MyClass());     

    accessor.Invoke("SomeMethod", packet); //Act

    // what will be the Assert? Since it is void
}

What will be the Assert? Since it is void, how can I write the assert?


Solution

  • Well given that in the example the method under test is making a change to its argument/dependency you could assert that the desired result of calling the function is that the packet's AllocatedAgency property is in fact not null

    [TestMethod]
    public void TestAllocatedAgency() { 
        //Arrange
        var packet = new Fixture().Create<PrimaryAllocationDP>(); //used AutoFixture here
        var sut = new MyClass();
        var accessor = new PrivateObject(sut);     
    
        //Act
        accessor.Invoke("SomeMethod", packet);
    
        //Assert
        Assert.IsNotNull(packet.AllocatedAgency);
    }