I am experiencing an issue during Testing. I have a class and properties like these ones
public class ViewModel {
....
public string Person
{
get
{
return _person;
}
set
{
if (_person != value)
{
_person = value;
RefreshStrategy();
OnPropertyChanged();
Somefield = string.Empty;
}
}
}
public BindingList<string> Strategies
{
get
{
return _lstStrategies;
}
}
Now RefreshStrategy, which is a huge method with several other objects, takes some strategies from a database and updates _lstStrategies according to the Person selected. Now everything is working fine on runtime but when I try to test it, I have an issue:
[TestClass]
public class ViewModelTest
{
private ViewModel _viewobj;
public ViewModelTest()
{
_viewobj = new ViewModel();
}
[TestMethod]
public void TestStrategies()
{
_viewobj.Person = "Test";
Assert.AreEqual("AAAA", _viewobj.Strategies[0]);
}
private void RefreshStrategies()
{
Logger.log(Logger.INFO, $"GOT STRATEGIES");
// Reload these default options every time the form loads, in case some applications settings have been changed
if (String.IsNullOrEmpty(Trader) && !SettingsMgr.IsCompositePortfolio)
{
Person = Utility.SettingsMgr.PORTFOLIO_CODE;
}
if (!String.IsNullOrEmpty(Person))
{
string fund = _portfolios.Where((tt) => tt.PersonId == Person).Select(tt => tt.Fund).FirstOrDefault();
if (!string.IsNullOrEmpty(fund))
{
fund = "X";
}
var profitCenters = new List<ProfitCenter>();
try
{
Logger.log(Logger.INFO, $"Try to load strategies for {Person} {fund}");
var pci = DataMgr.getInstance().GetProfitCenterInfo(Person, fund);
Logger.log(Logger.INFO, $"HERE !!!");
profitCenters = pci?.profitCenters;
}
......
}
When I set Person = "Test", obviously RefreshStrategy() is called but inside all objects are null and it raises the exception "...attempt to dereference a null object reference". The simple Logger at the beginning is raising and if I comment it out Utility.Setting is reasing and so on
If I do a trivial test on this class like testing a simple variable is passing and this is telling me that all references in the test project are fine.
What am I doing wrong? Do I mock the entire method? And if yes how if it is private?
RefreshStrategyinto separate class. Create interface for it and then you can simply mock the interface.