public void CreateResponse_DoingConneg_RetrievesContentNegotiatorFromServices() { // Arrange var context = new DefaultHttpContext(); var services = new Mock <IServiceProvider>(); services .Setup(s => s.GetService(typeof(IContentNegotiator))) .Returns(Mock.Of <IContentNegotiator>()) .Verifiable(); var options = new WebApiCompatShimOptions(); options.Formatters.AddRange(new MediaTypeFormatterCollection()); var optionsAccessor = new Mock <IOptions <WebApiCompatShimOptions> >(); optionsAccessor.SetupGet(o => o.Value).Returns(options); services .Setup(s => s.GetService(typeof(IOptions <WebApiCompatShimOptions>))) .Returns(optionsAccessor.Object); context.RequestServices = services.Object; var request = CreateRequest(context); // Act request.CreateResponse(HttpStatusCode.OK, CreateValue()); // Assert services.Verify(); }
private static IServiceProvider CreateServices( IContentNegotiator contentNegotiator = null, MediaTypeFormatter formatter = null) { var options = new WebApiCompatShimOptions(); if (formatter == null) { options.Formatters.AddRange(new MediaTypeFormatterCollection()); } else { options.Formatters.Add(formatter); } var optionsAccessor = new Mock <IOptions <WebApiCompatShimOptions> >(); optionsAccessor.SetupGet(o => o.Value).Returns(options); var services = new Mock <IServiceProvider>(MockBehavior.Strict); services .Setup(s => s.GetService(typeof(IOptions <WebApiCompatShimOptions>))) .Returns(optionsAccessor.Object); if (contentNegotiator != null) { services .Setup(s => s.GetService(typeof(IContentNegotiator))) .Returns(contentNegotiator); } return(services.Object); }