Created
June 7, 2017 06:35
-
-
Save mallibone/207a202f9f169687c66804a86d93420d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using Moq; | |
using Xunit; | |
namespace CSharpSample | |
{ | |
public interface IDirectory | |
{ | |
string[] GetFileSystemEntries(string path); | |
// ... more implemented methods | |
} | |
public class DirectoryWrapper : IDirectory | |
{ | |
string[] IDirectory.GetFileSystemEntries(string path) | |
{ | |
string[] entries = Directory.GetFileSystemEntries(path); | |
return entries; | |
} | |
} | |
public class DirectoryListings | |
{ | |
private readonly IDirectory _directory; | |
public DirectoryListings(IDirectory directory) | |
{ | |
if(directory == null) throw new ArgumentNullException(); | |
_directory = directory; | |
} | |
public bool HasEntryInDirectory(string entryName, string path) | |
{ | |
return _directory.GetFileSystemEntries(path).Contains(entryName); | |
} | |
} | |
public class DirectoryListingsTest | |
{ | |
private string[] _mockEntries = new string[4]{"Alpha","Bravo","Charlie","Delta"}; | |
[Fact] | |
public void HasEntryInDirectory_ReturnsTrueIfTheAnswerContainsThePath() | |
{ | |
var directoryMock = new Mock<IDirectory>(); | |
directoryMock.Setup(m => m.GetFileSystemEntries(It.IsAny<string>())).Returns(_mockEntries); | |
var directoryListing = new DirectoryListings(directoryMock.Object); | |
Assert.True(directoryListing.HasEntryInDirectory("Bravo", "Some/Paht/That/Does/Not/Matter")); | |
} | |
[Fact] | |
public void HasEntryInDirectory_ReturnsFalseIfTheAnswerDoesNotContainsThePath() | |
{ | |
var directoryMock = new Mock<IDirectory>(); | |
directoryMock.Setup(m => m.GetFileSystemEntries(It.IsAny<string>())).Returns(_mockEntries); | |
var directoryListing = new DirectoryListings(directoryMock.Object); | |
Assert.False(directoryListing.HasEntryInDirectory("Echo", "Some/Paht/That/Does/Not/Matter")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment