Guest User

Untitled

a guest
Jun 24th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. //obviously not thread safe and lots of other things. But this illustrates the point that
  2. //Dispatcher can be stubbed and tested against to ensure a log entry was made.
  3.    public static class Logger
  4.    {
  5.       public static ILoggerDispatcher Dispatcher { get; set; }
  6.  
  7.       public static void Log(string message, Exception exception)
  8.       {
  9.          if (Dispatcher != null)
  10.          {
  11.             Dispatcher.Log(message, exception);
  12.          }
  13.       }
  14.  
  15.       public static void Log(LogLevel level, string message)
  16.       {
  17.          if (Dispatcher != null)
  18.          {
  19.             Dispatcher.Log(message);
  20.          }
  21.       }
  22.    }
  23.  
  24. //MSpec test example
  25.       protected ILoggerDispatcher fakeLogDispatcher;
  26.  
  27.       Establish context = () =>
  28.       {
  29.          fakeLogDispatcher = A.Fake<ILoggerDispatcher>();
  30.          Automation.Logger.Dispatcher = fakeLogDispatcher;
  31.       };
  32.  
  33. //An assertion somewhere
  34.       It should_log_that_the_attempt_failed = () => A.CallTo(() => fakeLogDispatcher.Log(A<string>._)).MustHaveHappened();
Add Comment
Please, Sign In to add comment