TDD – Test Driven Development

Testing i C#

I got used to TDD in my previous job – and to many good principles with me.


– and also some basic UnitTesting!

How to test private members

First let me make it clear that you should not need to test private methods. It means your code should be designed differently. But if you deal with legacy code, it can be convenient to have the option to test private methods.

There are two options:

  1. InternalsVisibleToAttribute
  2. PrivateObject –

1. InternalsVisibleToAttribute

if you want to have access to MyAssembly in you test assembly (MyAssembly.Test) all you need in AssemblyInfo.cs for MyAssembly is the name of the test assembly like this:

[assembly: InternalsVisibleTo("CompanyName.Project.MyAssembly.Test")]

2. PrivateObject



var privateObjectToTest = new PrivateObject(new SetupDialog());
Object[] arguments = new[] {dialogConfigHelper};
string privateMethodToTest = "LoadDialogs";
object result = privateObjectToTest.Invoke(privateMethodToTest, arguments);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s