Skip to main content
Tweeted twitter.com/StackSoftEng/status/908185103733575682
added code formatting & syntax highlighting.
Source Link
amon
  • 136k
  • 27
  • 295
  • 386

Unit testing private calls, or not.

I have this method, which has multiple private calls; GetConfigStatusescalls: GetConfigStatuses(), ApplyFilters ApplyFilters(), GetConfigListInfoGetConfigListInfo().

So the only thing I can actually Verify and Assert in this method is the GetPackagesGetPackages() call. That's at least how I see it.

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) { //... } } 
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 IStorageRepositoryIStorageRepository). The GetPackagesGetPackages() returns a ListList<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); } 
[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); } 

Unit testing private calls, or not.

I have this method, which has multiple private calls; GetConfigStatuses, ApplyFilters, GetConfigListInfo.

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.

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.

[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); } 

Unit testing private calls, or not

I have this method, which has multiple private calls: GetConfigStatuses(), ApplyFilters(), GetConfigListInfo().

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.

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); } 
Source Link
frostings
  • 658
  • 1
  • 5
  • 9

Unit testing private calls, or not.

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.

[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); }