Maderas
Maderas

Reputation: 241

How to create type safety on a PrivateObject in C#

I have discovered a fantastic way to unit test private methods.

This is great except I don't like how the method name is typed in as a string. Is there a way to create a "safety net?" I want to type the method name so that the compiler can throw a compiler time error if the method does not exist on an object.

Private method:

public class BankAccount
{
    //Private method to test
    private bool VerifyAmount(double amount)
    {
        return (amount <= 1000);
    }
}

Unit Test:

[TestMethod()]        
public void VerifyAmountTest()
{
    //Using PrivateObject class
    PrivateObject privateHelperObject = new PrivateObject(typeof(BankAccount));                             
    double amount = 500F;
    bool expected = true;
    bool actual;
    actual = (bool)privateHelperObject.Invoke("VerifyAmount", amount);            
    Assert.AreEqual(expected, actual);            
}

I know that some people think that we should not unit test private methods. That is not the purpose of this question so let's not discuss that question and stay on topic.

Upvotes: -5

Views: 307

Answers (2)

JuanR
JuanR

Reputation: 7783

When you unit-test a class, you are essentially putting your consumer hat and calling the exposed methods of the class to verify that the class does what it claims to do.

For instance, consider this example using your BankAccount class:

public class BankAccount
 {
     public Widthdrawal WithdrawMoney(double amount)
     {
          if(!VerifyAmount(amount))
               throw new InvalidOperationException("Minimum dispensed is $1,000!");
          //Do some stuff here
          return new Withdrawal(1000);
     }
     private bool VerifyAmount(double amount)
     {
         return (amount <= 1000);
     }

 }

You can then test for a few things. For instance:

  1. That a valid amount results in a withdrawal.
  2. That an invalid amount results in an invalid operation exception.

Your tests:

[TestMethod]
public void Verify_Valid_Amount_Results_In_Widtdrawal()
{
     var bankAccount = new BankAccount();
     var withdrawal = bankAccount.WithdrawMoney(1200);
     Assert.IsNotNull(withdrawal);
     Assert.AreEqual(1200, withdrawal);
}


[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void Verify_Valid_Amount_Results_In_Exception()
{
     var bankAccount = new BankAccount();
     var withdrawal = bankAccount.WithdrawMoney(800);
}

As you can see, you test the funcionality that uses the private method, not the private method itself.

If it's important for you to verify that method, you can either make it public or abstract the concept of amount verification to another class that exposes this method and can be unit tested separately.

Upvotes: 0

Dewfy
Dewfy

Reputation: 23624

Am I right that you want check presence of private method on .Net object?

Then pick one of the following cases to extract any method from instance:

Case 1 If you don't care about method signature:

var typeOfObj = typeof(BancAccount)
               .GetMethods(
                 BindingFlags.NonPublic | 
                 BindingFlags.Instance)
               .Any( method => method.Name == testedName )

Case 2 If you need specify exact signature then use - typeof(BancAccount).GetMethod(testedName, <types of arguments>)

Upvotes: -2

Related Questions