Accessing private fields
First I'm sorry if question is already been asked but I don't find anyone like the mine (but I assume it is a pretty common question) So I'm trying to do some unit tests, and the first one is already problematic..
I have to test the constructor of my class, in the constructor I set an instance of a private field.. So how do I test if this PRIVATE field is not null? (because I assume is that what I have to test)--> To test :
public BUDGET_MANAGER()
{
this.budget_provider = new BUDGET_PROVIDER();
}
--> Test Mehod :
[TestMethod()]
public void BUDGET_MANAGERConstructorTe开发者_如何转开发st1()
{
BUDGET_MANAGER target = new BUDGET_MANAGER();
Assert.IsNotNull(??,"the provider is not instancied");
}
How Can I do that? Thanks for help, I'm pretty lost in unit testing..
In your unit testing you really shouldn't have to test anything that's private to a class. The private, internally-only known members are part of the implementation of the class and not part of its exposed (and tested) functionality.
Basically, think of the externally-visible members of the class as its "contract." That defines its actual type, what everything else sees. And that is the functionality being tested. Internal (private) members aren't known outside of the class for very good reason, a different class can implement that same "contract" (or interface) in a different way, with different private members.
What you're testing is the visible functionality, the contract or interface.
Unit tests should not inspect private data. They should be testing that the behaviour defined by the interface of your class works, independent of any implementation details.
A typical test would call the constructor, and then afterwards call a public property or method and check that the result is as expected. Testing in this way means that if you later change your implementation to (for example) lazily construct a BudgetProvider only when it is needed, then all your tests will still work. Implementation details such as when a private member is or is not null is not relevant to clients of your class and therefore there is no need to test it in your unit tests.
If your using mstest, right click the original class and press create test accessor, select your test project. Then test this condition using the accessor (should show up in intellisense).
I'm not sure its a very good idea though, as the other posters have said. You would be making the implementation more difficult to change.
You should't really test any private variables of your classes.
Why would you like to test the constructor itself? It would make sense to test it if there is some logic. For example - you only construct the object if the given parameters are correct and you do validation before you create the object. Otherwise, construct the object and verify that it behaves as expected. Presumably if the constructor works incorrectly the object's behaviour will be incorrect as well.
Also resist the temptation of exposing private fields as properties just to validate that they were set correctly in the constructor.
Other guys mentioned what you should not to do when using unit testing.
I'll try to find a way to do what you want (you still need to test your constructor):
public BUDGET_MANAGER()
{
try
{
this.budget_provider = new BUDGET_PROVIDER();
}
catch {}
if (this.budget_provider == null)
throw new NullReferenceException("Budget provider is null !");
}
精彩评论