NSubstitute icon indicating copy to clipboard operation
NSubstitute copied to clipboard

.When(...).Throws gives a CouldNotSetReturnDueToNoLastCallException

Open 304NotModified opened this issue 1 year ago • 1 comments

Describe the bug This is of course a bug in the user code, as we should use Throw instead of Throws. But the exception is at least not clear

And it's confusing, IntelliSense in VS 2022 also suggest throws:

image

To Reproduce See https://github.com/nsubstitute/NSubstitute/pull/802

Expected behaviour

  • Just work
  • Or give a clear error

Environment:

  • NSubstitute version: 5.1
  • NSubstitute.Analyzers version: c# 1.0.17
  • Platform: .NET 8

Additional context

The analyzer gives a error, but that one is still a bit confusing

image

There are multiple options:

  1. Or give a clear error
  2. Or also implement Throws on WhenCalled (not sure if this is a binary breaking change?)
  3. Or delegate Throws to WhenCalled.Throw - this is difficult as Throws returns a ConfiguredCall and Throw is void

304NotModified avatar Apr 30 '24 21:04 304NotModified