【发布时间】:2020-05-31 20:26:24
【问题描述】:
我正在尝试使用 FluentAssertions 库创建一些断言类。这是断言代码:
public AndConstraint<MyTaskAssertions> Work(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.WithExpectation("Expected {context:mytask} to work{reason}, ")
.Given(() => Subject)
.ForCondition(x => x.Works)
.FailWith("but it doesn't");
return new AndConstraint<MyTaskAssertions>(this);
}
这是我的测试:
var t = new MyTask {Works=false};
t.Should().Work();
一切正常,除了变量名t,“mytask”显示在异常消息上:
预计 mytask 可以工作,但它没有
我已经阅读了文档上的Extensibility 页面,并且我还检查了内置断言的源代码,但我仍然不确定异常消息究竟缺少什么来显示实际变量名称而不是“上下文:”占位符之后的内容。
【问题讨论】: