stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
558 stars 106 forks source link

Ability to mock the CorrelationContext. #38

Closed Wind010 closed 4 years ago

Wind010 commented 5 years ago

Would like to mock the ICorrelationContextAccessor to provide back a CorrelationContext, so I can access the CorrelationId property. Unfortunately there CorrelationContext does not inherit from an interface and the constructor is internal.

filip7sr commented 5 years ago

Try this in stead:

var httpClient = new HttpClient(handlerMock.Object);
httpClient.BaseAddress = new Uri("https://localhost:44315/");

var accessor = new CorrelationContextAccessor();
var factory = new CorrelationContextFactory(accessor);
var context = factory.Create(Guid.NewGuid().ToString(), "X-Correlation-ID");
_yourHttpClient = new EmployeePlanningClient(httpClient, accessor);
stevejgordon commented 4 years ago

Added support for this by making the ctor public and adding a sample test project