FluentAssertions is a great library but often I am very frustrated when some code in lambda is not working as expected and I cannot debug it. Especially when lambda is complicated.
payload.Resource.Relations.Removed.Should().NotBeNull()
.And.HaveCount(2)
.And.AllBeOfType<ResourceRelation>()
.And.OnlyContain(rel =>
rel.RelationType.MatchTo(RelationType.ArtifactLink) &&
rel.Href.AbsoluteUri.StartsWith(VsTfsSchema.GitPullRequestId));
In this case, I would like to set a breakpoint into inside OnlyContain(...)
lambda and debug it. But this is not possible - breakpoint is set always at the whole statement. I suppose that the reason is that lambdas in FluentAssertions are expressions.
Is there any way how to achieve this?
Edit: Extracting lambda as local variable does not help. Behavior is the same.
System.Linq.Expressions.Expression<Func<ResourceRelation, bool>> predicate = rel =>
rel.RelationType.MatchTo(RelationType.ArtifactLink) && rel.Href.AbsoluteUri.StartsWith(VsTfsSchema.GitPullRequestId);
payload.Resource.Relations.Removed.Should().NotBeNull()
.And.HaveCount(2)
.And.AllBeOfType<ResourceRelation>()
.And.OnlyContain(predicate);
Edit2: Here is really simple and verifiable example. You cannot put a breakpoint into num == 1
, nor extract it as local function, nor display it at watch.
[Fact]
public void SimpleLambdaTest()
{
int[] nums = Enumerable.Range(1, 10).ToArray();
nums.Should().OnlyContain(num => num == 1);
}
答案 0 :(得分:2)
您可以将表达式主体提取到静态函数中,在其中可以设置断点。
请注意,EqualsOne
不能是局部函数,也不能作为方法组传递。
[Fact]
public void SimpleLambdaTest()
{
int[] nums = Enumerable.Range(1, 10).ToArray();
nums.Should().OnlyContain(num => EqualsOne(num));
}
private static bool EqualsOne(int num)
{
// You can put a break point here
return num == 1;
}
答案 1 :(得分:1)
尽管这与FluentAssertions无关,但我经常与Jetbrains Rider一起这样做。当您尝试设置断点时,它会问您在哪里。在整行中,在单个lambda上,等等。我已经有近两年没有使用Visual Studio进行调试了,所以我不知道它是否可以处理。
答案 2 :(得分:0)
即使是这样,如果您在折行上按F11键,调试也应该带您到lambda表达式。否则,您仍然可以使用Add Watch
或Quick Watch
工具(选择lambda表达式->右键单击并选择Quick Watch)