Answer the question
In order to leave comments, you need to log in
How to test methods that work with private or protected fields?
There is such a simple code.
class Worker { }
interface IOffice
{
void AddWorker(Worker worker);
Worker[] GetWorkers();
}
class Office : IOffice
{
private List<Worker> _workers;
public Office()
{
_workers = new List<Worker>();
}
public void AddWorker(Worker worker)
{
_workers.Add(worker);
}
public Worker[] GetWorkers()
{
return _workers.ToArray();
}
Answer the question
In order to leave comments, you need to log in
1. Not true. You will use GetWorkers but test AddWorker. You can write a separate test on GetWorkers.
PS: Testing private fields is bad practice. You need to test the expected behavior of the accessible from outside the method, i.e. public. You do not need to test the internal implementation, you need to test what it will give as an output.
Don't use CLR, C# and computer.
You should only use AddWorker.
Do it on paper. Fountain pen. With ink.
Yes, in any test I will need the _workers field. How to get it?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question