0

I'm using FluentAssertions 5.3.0 run under NCrunch 3.14.0.1. When a test fails, I often seen this error as FluentAssertions tries to generate a report message...

System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex at System.String.Substring(Int32 startIndex, Int32 length) at FluentAssertions.CallerIdentifier.ExtractVariableNameFrom(StackFrame frame) at FluentAssertions.CallerIdentifier.DetermineCallerIdentity() at FluentAssertions.Execution.AssertionScope.GetIdentifier() at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) at FluentAssertions.Numeric.NumericAssertions`1.Be(T expected, String because, Object[] becauseArgs) at TableTests.ConsolidatedMovementsTester.AlternativingMovementStatesAreRecognised() in ...test.cs

The error is sporadic - often it will disappear if the test is rerun (in which case, a proper error report is shown). I've seen this in the last couple of versions of both FluentAssertions and NCrunch and it appears on multiple machines (all running some flavour of Visual Studio 2017 Professional).

Here's an example of the latest call to bite me...

results.Count(c => c.IsStationary).Should().Be(5);

where results is an ImmutableArray of POCOs

In this case, right-clicking on the failing test and selecting "rerun in new test runner" in the ncrunch window, caused the test to re-run and FA to emit the correct report.

Can anyone suggest a workaround for this?

NeilMacMullen
  • 3,339
  • 2
  • 20
  • 22
  • What does the call to FA look like (specifically)? – Dennis Doomen May 01 '18 at 17:22
  • I see this in a variety of calls - I've added one to the question. – NeilMacMullen May 01 '18 at 17:23
  • 1
    Beats me. If you check [the code to extract the name](https://github.com/fluentassertions/fluentassertions/blob/master/Src/FluentAssertions/CallerIdentifier.cs#L76), I can't think of a scenario where the start index can be larger than the string itself. – Dennis Doomen May 01 '18 at 17:37
  • My suspicion is that this is some kind of race condition based on the way Ncrunch is continually compiling and running tests in the background. Presumably StackFrame is getting corrupted (or the wrong one is passed in)? – NeilMacMullen May 01 '18 at 17:44
  • Hmm, there's no thread-sensitive code there. But I do see a possibility that the first Substring can fail if the stackframe is corrupt or dynamic. – Dennis Doomen May 01 '18 at 17:49
  • Hmm - isn't the problem that GetSourceCodeLineFrom is retrieving the filename from the stackframe then going away to read this? In the Ncrunch scenario, the tests are being compiled and run literally on every keystroke so there is no guarantee that the file/line you read matches the one mentioned in the stackframe? (Not saying it's an unreasonable assumption - maybe Ncrunch should do a better job of isolating the environment...) – NeilMacMullen May 01 '18 at 17:52
  • https://github.com/fluentassertions/fluentassertions/pull/841 – Dennis Doomen May 01 '18 at 18:20
  • Would you be willing to try this package? If it's fine, I'll release it. https://ci.appveyor.com/api/buildjobs/o0trdip6gjk4447r/artifacts/Artifacts%2FFluentAssertions.5.3.1.nupkg – Dennis Doomen May 02 '18 at 05:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170213/discussion-between-dennis-doomen-and-neilmacmullen). – Dennis Doomen May 02 '18 at 11:23

0 Answers0