Access to private fields

At first I apologize if the question has already been asked, but I do not find someone like mine (but I assume this is a fairly common question) Therefore, I am trying to perform some unit tests, and the first one is already problematic.

I need to check the constructor of my class, in the constructor I install an instance of a private field. So, how can I check if this PRIVATE field is not null? (because I assume that what I should check) β†’ To check:

public BUDGET_MANAGER() { this.budget_provider = new BUDGET_PROVIDER(); } 

-> Test Mehod:

  [TestMethod()] public void BUDGET_MANAGERConstructorTest1() { BUDGET_MANAGER target = new BUDGET_MANAGER(); Assert.IsNotNull(??,"the provider is not instancied"); } 

How can i do this? Thanks for the help, I'm pretty lost in unit testing.

+1
source share
5 answers

In testing your device, you really do not need to check anything that is private to the class. Private, internal only known members are part of the class implementation, and not part of its exposed (and verified) functionality.

Basically, think of the externally visible members of the class as its "contract." This determines its actual type, which sees everything else. And this is proven functionality. Internal (private) members are not known outside the class for a very good reason; another class may implement the same β€œcontract” (or interface) differently with different private members.

What you are testing is visible functionality, a contract, or an interface.

+4
source

Unit tests should not verify personal data. They should verify that the behavior defined by the interface of your class does not depend on any implementation details.

A typical test will call the constructor and then call the public property or method and verify that the result is as expected. Testing this way means that if you later change your implementation (for example), lazily build the BudgetProvider only when necessary, all your tests will still work. Implementation details, for example, when a private member is or is not null, are not related to clients of your class, and therefore there is no need to test it in your unit tests.

+2
source

If you use mstest, right-click the source class and click the create test accessor button, select your test project. Then check this condition using accessor (should appear in intellisense).

I'm not sure this is a very good idea, as other posters have said. You would make it difficult to change the implementation.

+1
source

You should not check any private variables of your classes.

Why do you want to test the constructor itself? It would be wise to test it if there is any logic. For example, you create an object only if the specified parameters are correct, and you perform a check before creating the object. Otherwise, create an object and make sure that it behaves as expected. Presumably, if the constructor does not work correctly, the behavior of the object will also be incorrect.

Also resist the temptation to expose private fields as properties only to verify that they were set correctly in the constructor.

+1
source

Other guys mentioned what you should not do when using unit testing.

I will try to find a way to do what you want (you still need to check 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 !"); } 
0
source

All Articles