I have created a very simple log and now I want to unit test it. Here is the
class
public class EventLogger : IEventLogger
{
private string _logDirectory = @"C:\\EventLog.txt";
public string LogDirectory
{
get
{
return _logDirectory;
}
set
{
if (value.EndsWith(".txt"))
_logDirectory = value;
else
throw new ArgumentOutOfRangeException(value, "Log extension was not txt");
}
}
public void LogEvent(DateTime date, EventCategorie cat,EventType eventType,List<ITrack> tracks)
{
if (FS == null)
FS = File.Open(LogDirectory, FileMode.OpenOrCreate, FileAccess.Write);
if (!tracks.Any())
throw new ArgumentException("There was no tracks in the list",nameof(tracks));
string message = date.ToShortDateString() + " --- " + cat + " --- " + eventType + " --- ";
foreach (var track in tracks)
message = message + track.Tag + " & ";
message = message.Remove(message.Length -3) + Environment.NewLine;
File.AppendAllText(LogDirectory,message);
}
I can test on the file extension given and I have done that but I was not sure as of how I could unit test my LogEvent method as I do have a dependency to the file system. I started out googling and most people said that you should use property injection with a filestream. I just felt silly when I startet adding that to the original implementation. I then found this and thought this was one of the cases where you can assume that the static method AppendAllText works, or will I need to create a filestream property just to call this one method So I can test it?
Aucun commentaire:
Enregistrer un commentaire