Бөлісу құралы:


Customizing the Logger for Unit Testing

Typical Goals

To effectively unit test your classes, you may want to bypass the default logging and tracing behavior of the SharePoint Logger and instead write all logging and tracing information to a mock ILogger implementation. Depending on the primary motivation for your test, the mock logger might write to the Windows event log and the ULS trace log as normal, write to a test-specific file stream, or simply set a public property that show that the logger was invoked as expected.

Solution

To configure the SharePoint Logger for unit testing, you need to replace the default SharePoint Logger instantiation with your mock ILogger implementation when you initialize the unit test. To do this, you must first create a new, test-specific instance of the SharePoint Service Locator. You can then register your mock ILogger implementation with the service locator instance for the duration of the test.

This approach ensures that you do not have to modify the code that you are testing. Your code can continue to use the LogToOperations and TraceToDeveloper methods that are defined by the ILogger interface. Behind the scenes, the SharePoint Service Locator simply switches the default ILogger implementation with your mock ILogger implementation.

You need to be familiar with the SharePoint Service Locator to understand how and why this solution works. For more information, see The SharePoint Service Locator.

Providing a Mock ILogger Implementation

Your first step should be to develop the mock implementation of ILogger. The following code shows an example implementation.

public class MockLogger : ILogger
{
   // Define a public property for the message. 
   // Your unit tests can query this property
   // to validate that the method was called. 
   public string LogToOperationsCalledWithMessage {get; set;};

   public void LogToOperations (string message)
   {
      LogToOperationsCalledWithMessage = message;  
   }
}

Your next step is to register your mock implementation with the SharePoint Service Locator for the duration of your unit test. The registration takes place in the test initialization method.

public class MyFixture
{

   [TestMethod]
   public void TestMyWidgetThatUsesLogging()
   {
// Arrange
       ActivatingServiceLocator locator = new ActivatingServiceLocator();
       locator.RegisterTypeMapping<ILogger, MockLogger>
                                               (InstantiationType.AsSingleton);

       SharePointServiceLocator.ReplaceCurrentServiceLocator(locator);

       var logger = SharePointServiceLocator.GetCurrent().GetInstance<ILogger>()
                                                                                       as MockLogger;
       string expectedMessage = “some message you expect your logic to log”;

       // Act
       … run your logic that uses logging

       //Assert
       Assert.AreEqual(expectedMessage, logger.LogToOperationsCalledWithMessage);
    }

    // ...
}

For more information about how to use alternative ILogger implementations, see Using Custom Logger Classes. For more information about how to use the SharePoint Service Locator to isolate code for unit tests, see Testing Classes in Isolation.

Usage Notes

In the preceding code example, notice how the mock ILogger implementation is registered as a singleton service. This ensures that every call to the service locator returns the same object, which can help to provide consistency for your unit test.