I have this method, which has multiple private calls: GetConfigStatuses()
, ApplyFilters()
, GetConfigListInfo()
.
Since these do not expose anything public I cannot test them like I would a public. And I am not changing access level, that would be a smell.
So the only thing I can actually Verify and Assert in this method is the GetPackages()
call. That's at least how I see it.
But I am not sure, because I am fairly new to mocking things up :)
Below is the method I am testing, and one of my tests.
Does things look ok ? Thank you :)
public IEnumerable<UIConfigListInfo> GetUiConfigs(string segment, UIConfigInfo uiConfigInfo)
{
try
{
var configStatus = string.IsNullOrEmpty(uiConfigInfo.Status) ? "Active" : uiConfigInfo.Status;
var configListInfoList = new List<UIConfigListInfo>();
foreach (var currentConfigStatus in GetConfigStatuses(configStatus))
{
var filters = ApplyFilters(segment, uiConfigInfo.Name.ToLowerInvariant(), currentConfigStatus.ToLowerInvariant(), uiConfigInfo.Version);
var packages = _storageRepository.GetPackages(filters, Order.Descending, configStatus).ToList();
foreach (Package package in packages)
{
try
{
//TODO: comment this.
var configListInfo = GetConfigListInfo(package, uiConfigInfo.Version, uiConfigInfo.Name, currentConfigStatus);
//TODO: comment this.
GetConfigDefinition(package.Data, configListInfo);
configListInfoList.Add(configListInfo);
}
catch (Exception ex)
{
//TODO: log! Throw ?
}
}
}
return configListInfoList;
}
catch (Exception ex)
{
//...
}
}
And the test below. It's important to notice my Service class only has one public dependency (the IStorageRepository
). The GetPackages()
returns a List<Package>
.
[TestMethod]
public void TestMethod1()
{
//Arrange
var mockStorageRepository = new Mock<IStorageRepository>();
mockStorageRepository.Setup(s => s.GetPackages(It.IsAny<List<string>>(), Order.Ascending, "Active"));
var sut = new UIConfigService(mockStorageRepository.Object);
//Act
sut.GetUiConfigs("somesegment", new UIConfigInfo { Name = "TestFun" });
//Assert
mockStorageRepository.Verify(mock => mock.GetPackages(It.IsAny<List<string>>(), Order.Descending, "Active"), Times.Once);
}