From 4b0d4ba4e79c1a2e22ccf131d1abdbf113d78b6a Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 11 Aug 2019 16:08:38 +0800 Subject: Fix the bug in assert message and add a formatter. --- .../Helpers/AssertionResponseExtensions.cs | 31 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'Timeline.Tests/Helpers/AssertionResponseExtensions.cs') diff --git a/Timeline.Tests/Helpers/AssertionResponseExtensions.cs b/Timeline.Tests/Helpers/AssertionResponseExtensions.cs index b56340ea..38617b92 100644 --- a/Timeline.Tests/Helpers/AssertionResponseExtensions.cs +++ b/Timeline.Tests/Helpers/AssertionResponseExtensions.cs @@ -1,5 +1,6 @@ using FluentAssertions; using FluentAssertions.Execution; +using FluentAssertions.Formatting; using FluentAssertions.Primitives; using Newtonsoft.Json; using System; @@ -9,9 +10,31 @@ using Timeline.Models.Http; namespace Timeline.Tests.Helpers { + public class HttpResponseMessageValueFormatter : IValueFormatter + { + public bool CanHandle(object value) + { + return value is HttpResponseMessage; + } + + public string Format(object value, FormattingContext context, FormatChild formatChild) + { + string newline = context.UseLineBreaks ? Environment.NewLine : ""; + string padding = new string('\t', context.Depth); + + var res = (HttpResponseMessage)value; + return $"{newline}{padding} Status Code: {res.StatusCode} ; Body: {res.Content.ReadAsStringAsync().Result} ;"; + } + } + public class HttpResponseMessageAssertions - : ReferenceTypeAssertions + : ReferenceTypeAssertions { + static HttpResponseMessageAssertions() + { + Formatter.AddFormatter(new HttpResponseMessageValueFormatter()); + } + public HttpResponseMessageAssertions(HttpResponseMessage instance) { Subject = instance; @@ -23,7 +46,7 @@ namespace Timeline.Tests.Helpers { Execute.Assertion.BecauseOf(because, becauseArgs) .ForCondition(Subject.StatusCode == expected) - .FailWith("Expected status code is {}, but found {}.", expected, Subject.StatusCode); + .FailWith("Expected status code of {context:HttpResponseMessage} to be {0}{reason}, but found {1}.\nResponse is {2}.", expected, Subject.StatusCode, Subject); return new AndConstraint(Subject); } @@ -37,7 +60,7 @@ namespace Timeline.Tests.Helpers } catch (Exception e) { - a.FailWith("Failed to read response body. Exception is {}.", e); + a.FailWith("Failed to read response body of {context:HttpResponseMessage}{reason}.\nException is {0}.", e); return new AndWhichConstraint(Subject, null); } @@ -48,7 +71,7 @@ namespace Timeline.Tests.Helpers } catch (Exception e) { - a.FailWith("Failed to convert response body to {}. Exception is {}.", typeof(T).FullName, e); + a.FailWith("Failed to convert response body of {context:HttpResponseMessage} to {0}{reason}.\nResponse is {1}.\nException is {2}.", typeof(T).FullName, Subject, e); return new AndWhichConstraint(Subject, null); } } -- cgit v1.2.3