Stub protected List in base class for unit-test
I want to test if a method adds a object to a list in the base class.
My problem is that the list is protected so i can´t populate it in my test
arrangement. Therefore the list is empty and my method fails to add the
object at a specific index.
How do i make a stub list and use it instead of the real base class list
without making it public?
Im using NUnit and Moq.
Here´s some code to imitate my problem:
public class Person
{
public string Name { get; set; }
readonly List<PhoneNumber> _numbers = new List<PhoneNumber>();
public void AddNumber(PhoneNumber phoneNumber)
{
_numbers.Add(phoneNumber);
}
}
public class PhoneNumber
{
public int Number { get; set; }
}
public class BaseClass
{
protected List<Person> Persons = new List<Person>();
}
public class DerivedClass : BaseClass
{
public void AddNumberToPersons(int index, PhoneNumber phoneNumber)
{
Persons[index].AddNumber(phoneNumber); // <-- Need to test if
// this gets called without
errors
}
}
[Test]
public void
AddNumberToPersons_WhenAddingNumberToPersons_NumberGetsAddedToList()
{
// I think i need to use a stub list or else the list is empty
// and index is out of range.
// Assert that number is added to the list.
}
Im new to unit testing so thanks for all the help i can get!
No comments:
Post a Comment