Implementing a ILogger interface to log data

Posted by Jon on Programmers See other posts from Programmers or by Jon
Published on 2012-03-21T18:41:55Z Indexed on 2012/03/21 23:39 UTC
Read the original article Hit count: 241

Filed under:
|
|
|
|

I have a need to write data to file in one of my classes.

Obviously I will pass an interface into my class to decouple it.

I was thinking this interface will be used for testing and also in other projects.

This is my interface:

//This could be used by filesystem, webservice
public interface ILogger
{
   List<string> PreviousLogRecords {get;set;}
   void Log(string Data);
}

public interface IFileLogger : ILogger
{
  string FilePath;
  bool ValidFileName;
}

public class MyClassUnderTest
{
  public MyClassUnderTest(IFileLogger logger) {....}
}

[Test]
public void TestLogger()
{
   var mock = new Mock<IFileLogger>();
   mock.Setup(x => x.Log(Is.Any<string>).AddsDataToList()); //Is this possible??

   var myClass = new MyClassUnderTest(mock.Object);

   myClass.DoSomethingThatWillSplitThisAndLog3Times("1,2,3");

   Assert.AreEqual(3,mock.PreviousLogRecords.Count);

}

This won't work I don't believe as nothing is storing the items so is this possible using Moq and also what do you think of the design of the interface?

© Programmers or respective owner

Related posts about c#

Related posts about design