Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
304NotModified opened this issue Apr 30, 2024 · 1 comment

Comments

@304NotModified
Copy link
Contributor

304NotModified commented Apr 30, 2024

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 #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 304NotModified changed the title When Throws gives a CouldNotSetReturnDueToNoLastCallException .When(...).Throws gives a CouldNotSetReturnDueToNoLastCallException Apr 30, 2024
@dtchepak
Copy link
Member

dtchepak commented May 5, 2024

I think adding Throws as an alias to Throw on WhenCalled might be the easiest way to do this (that should take precedence over the extension method). Could make as [Obsolete] with an explanation if we want to encourage the "correct" use here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants