Assert.AddValueFormatter 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| AddValueFormatter(Func<Func<Object,String>,Func<Object,String>>) |
注册责任链值格式化程序工厂。 工厂在链中接收 |
| AddValueFormatter<T>(Func<T,String>) |
注册一个自定义格式化程序,用于控制如何在结构化断言失败消息中呈现值 |
AddValueFormatter(Func<Func<Object,String>,Func<Object,String>>)
- Source:
- Assert.AddValueFormatter.cs
注册责任链值格式化程序工厂。 工厂在链中接收 next 格式化程序并返回新的格式化程序;返回的格式化程序应为它处理的值生成字符串,或者为它未处理的值生成一个字符串 next 。
注册的范围限定为当前异步流,并在释放返回 IDisposable 时删除。
[System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")]
public static IDisposable AddValueFormatter(Func<Func<object?,string>,Func<object?,string>> factory);
public static IDisposable AddValueFormatter(Func<Func<object?,string>,Func<object?,string>> factory);
[<System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")>]
static member AddValueFormatter : Func<Func<obj, string>, Func<obj, string>> -> IDisposable
static member AddValueFormatter : Func<Func<obj, string>, Func<obj, string>> -> IDisposable
Public Shared Function AddValueFormatter (factory As Func(Of Func(Of Object, String), Func(Of Object, String))) As IDisposable
参数
返回
其 IDisposable 处置将删除注册。
- 属性
例外
factory 是 null。
注解
如果需要检查多个类型(例如呈现任何实现标记接口的对象),或者需要对委派 next进行显式控制,请使用此重载。 对于常见的“设置特定类型格式”,首选 AddValueFormatter<T>(Func<T,String>)。
factory 是链生成器,而不是格式化程序本身:在注册处于活动状态时每次呈现值时都会调用它(每次呈现都会重新生成链,以便遵守其他注册的无序处置),而不仅仅是在注册时一次。 保持廉价和副作用免费;在工厂外部执行任何昂贵的设置,并让它返回一个在预计算状态上关闭的格式化程序。
适用于
AddValueFormatter<T>(Func<T,String>)
- Source:
- Assert.AddValueFormatter.cs
注册一个自定义格式化程序,用于控制如何在结构化断言失败消息中呈现值 T 。 注册的范围限定为当前异步流(由 AsyncLocal<T>该流提供支持),并在释放返回 IDisposable 时删除。
[System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")]
public static IDisposable AddValueFormatter<T>(Func<T,string?> formatter);
public static IDisposable AddValueFormatter<T>(Func<T,string?> formatter);
[<System.Diagnostics.CodeAnalysis.Experimental("MSTESTEXP", UrlFormat="https://aka.ms/mstest/diagnostics#{0}")>]
static member AddValueFormatter : Func<'T, string> -> IDisposable
static member AddValueFormatter : Func<'T, string> -> IDisposable
Public Shared Function AddValueFormatter(Of T) (formatter As Func(Of T, String)) As IDisposable
类型参数
- T
呈现的表示形式应自定义的类型。
参数
返回
其 IDisposable 处置将删除注册。
- 属性
例外
formatter 是 null。
示例
[TestMethod]
public void DateTimeFailureShowsKind()
{
using var _ = Assert.AddValueFormatter<DateTime>(dt => $"{dt:O} [{dt.Kind}]");
Assert.AreEqual(DateTime.UtcNow, DateTime.Now);
}
注解
格式化程序仅影响在失败消息中呈现值的方式。 它们不会更改断言执行的相等比较,例如 Assert.AreEqual。 若要自定义相等性,请使用现有 IEqualityComparer<T> 重载。
注册堆栈最新优先:首先咨询最近注册的格式化程序。 返回 null 的格式化程序将延迟到下一个格式化程序,并最终返回内置呈现器。
null 值始终由内置呈现器呈现 "null" ,永远不会传递给用户格式化程序。
常见注册站点及其有效范围:
- 在 :
[TestMethod]该测试方法(及其异步延续) 中。 -
[TestInitialize]:包含类中的每个测试。 -
[ClassInitialize]:包含类中的所有测试。 -
[AssemblyInitialize]:包含程序集中的所有测试。