Skip to content

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

Closed
@304NotModified

Description

@304NotModified

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions