Is it possible to get access to a private field in a unit test?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The way to get private fields or methods in general is to use Reflection. However, the unit test framework includes a helper class,
PrivateObject, to make this easier. See the docs. In general, when I’ve used this, I’ve ended up making an extension methods like the following:If you need to get private fields in a static class, however, you will need to go with straight up reflection.