public void SortPhoto_ValidDateTimeName_yyyymmDirectoryExist() { /// Description /// Tests that it is possible to rename and sort photo if the directories exist. /// Expectation /// Calls renameServiceMock.FindPhotoDateTime and renameServiceMock.RenameFile exactly ONCE! // Mock IRenameService var renameServiceMock = new Mock <IRenameService>(); // Creates a temp directory for testing var outputPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(outputPath); // Create temp photo var photoPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); File.Create(photoPath) .Close(); var photo = new Photo(photoPath); var today = DateTime.Today; var yearString = today.Year.ToString(); var monthString = today.Month.ToString("D2"); var expectedDateTime = today.ToString("yyyyMMdd_HHmmss"); var yyyymmPath = Path.Combine(outputPath, yearString, monthString); Directory.CreateDirectory(yyyymmPath); // Generate a temp IConfiguration object var configuration = CreateInMemoryConfiguration(outputPath); // Mock the IRenameService.RenameFile to create file in unknown directory renameServiceMock.Setup(mock => mock.RenameFile(It.IsAny <string>(), It.IsAny <string>())); // Mock the IRenameService.FindPhotoDateTime to return datetime name renameServiceMock.Setup(mock => mock.FindPhotoDateTime(It.IsAny <Photo>(), It.IsAny <string>())) .Returns(expectedDateTime); // Get the mocked objects var renameService = renameServiceMock.Object; // Construct the sort service and get the output directories structure var sortService = new SortService(logger, configuration, renameService); sortService.SortPhoto(photo); // Verify mock calls renameServiceMock.Verify(mock => mock.RenameFile(It.IsAny <string>(), It.IsAny <string>()), Times.Once); renameServiceMock.Verify(mock => mock.FindPhotoDateTime(It.IsAny <Photo>(), It.IsAny <string>()), Times.Once); // Assert that unknown directory does not exist var unknownDirectoryPath = Path.Combine(outputPath, UNKNOWN_DIRNAME); Assert.False(Directory.Exists(unknownDirectoryPath)); }