aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-11 16:08:38 +0800
committer杨宇千 <crupest@outlook.com>2019-08-11 16:08:38 +0800
commit4b0d4ba4e79c1a2e22ccf131d1abdbf113d78b6a (patch)
tree3e4e7de38b9f0e5832923a6bc1ce7872fc4370a5
parentec3743db2079e88a184c49564a7f46bbddf4b03e (diff)
downloadtimeline-4b0d4ba4e79c1a2e22ccf131d1abdbf113d78b6a.tar.gz
timeline-4b0d4ba4e79c1a2e22ccf131d1abdbf113d78b6a.tar.bz2
timeline-4b0d4ba4e79c1a2e22ccf131d1abdbf113d78b6a.zip
Fix the bug in assert message and add a formatter.
-rw-r--r--Timeline.Tests/Helpers/AssertionResponseExtensions.cs31
1 files changed, 27 insertions, 4 deletions
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<HttpResponseMessage, HttpResponseMessageAssertions>
+ : ReferenceTypeAssertions<HttpResponseMessage, HttpResponseMessageAssertions>
{
+ 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<HttpResponseMessage>(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<HttpResponseMessage, T>(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<HttpResponseMessage, T>(Subject, null);
}
}