Skip to content

Invoking should support Func<TInput, TResult> #872

@benjamin-bader

Description

@benjamin-bader

Description

Properties are merely functions in disguise, and it would be useful to be able to assert on exceptions that they might throw, for example that accessing a property of a disposed object throws an ObjectDisposedException.

This doesn't work today because a simple lambda that accesses a property is compiled as a Func<TInput, TResult>, but there is no corresponding overload that can accept this delegate type.

Complete minimal example reproducing the issue

class MyDisposable : IDisposable
{
  // the usual Disposable implementation elided here

  bool IsConnected
  {
    get
    {
      if (disposed) throw new ObjectDisposedException(GetType().Name);
      return _isConnected;
    }
  }
}

// Arrange
var subject = new MyDisposable();

// Act
subject.Dispose();

// Assert
subject.Invoking(it => it.IsConnected).Should().Throw<ObjectDisposedException>();

Expected behavior:

I would expect this to invoke the lambda, and verify that the specified exception was thrown.

Actual behavior:

Compilation failure.

Versions

Using FluentAssertions 5.4.1, running on .NET Core 2.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions