public void WriteSampleObjectUsingFormatter_UnwrapsAggregateException()
 {
     Mock<MediaTypeFormatter> bogusFormatter = new Mock<MediaTypeFormatter>();
     bogusFormatter.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true);
     bogusFormatter.Setup(f => f.WriteToStreamAsync(It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<TransportContext>())).Returns(() =>
     {
         throw new AggregateException(new FormatException("Invalid format."));
     });
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     InvalidSample sampleNotProvided = Assert.IsType<InvalidSample>(
         sampleGenerator.WriteSampleObjectUsingFormatter(
             bogusFormatter.Object,
             "hello world",
             typeof(string),
             new MediaTypeHeaderValue("text/json")
         ));
     Assert.Equal("An exception has occurred while using the formatter 'MediaTypeFormatterProxy' to generate sample for media type 'text/json'. Exception message: Invalid format.",
         sampleNotProvided.ErrorMessage);
 }
 public void WriteSampleObjectUsingFormatter_TryFormattingNonJsonSamples_DoesNotThrow()
 {
     Mock<MediaTypeFormatter> customFormatter = new Mock<MediaTypeFormatter>();
     customFormatter.Setup(f => f.CanWriteType(It.IsAny<Type>())).Returns(true);
     customFormatter.Setup(f => f.WriteToStreamAsync(It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<Stream>(), It.IsAny<HttpContent>(), It.IsAny<TransportContext>())).Returns(
     (Type type, object obj, Stream stream, HttpContent content, TransportContext context) =>
     {
         StreamWriter writer = new StreamWriter(stream);
         writer.Write("some\r\nnon <json> string");
         writer.Flush();
         TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
         tcs.SetResult(null);
         return tcs.Task;
     });
     HelpPageSampleGenerator sampleGenerator = new HelpPageSampleGenerator();
     TextSample sample = Assert.IsType<TextSample>(
         sampleGenerator.WriteSampleObjectUsingFormatter(
             customFormatter.Object,
             "hello world",
             typeof(string),
             new MediaTypeHeaderValue("text/json")
         ));
     Assert.Equal("some\r\nnon <json> string", sample.Text);
 }