question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoking should support Func<TInput, TResult>

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dennisdoomencommented, Mar 31, 2019

Alright, it seems that Invoking is the best option considering the various situations we need to cover.

1reaction
jnyrupcommented, Oct 1, 2018

As I understand the notation of Get it is mainly intended for getters, such as properties that only returns the value of the underlying backing field or methods that does very little computation. For other scenarios, e.g. IO operations, Find or Load might be more proper prefixes.

Func<T, TResult> can match a property, where Getting would be a fine name, but it also matches many other cases, where the semantics mismatches.

That’s why I dislike Getting since it imposes semantics we know nothing about.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How could I throw an exception using Fluent Assertions?
When using the code provided within the Github repository for this project, I noticed that I am unable to make use of a...
Read more >
Using Fluentassertions With Xunit Theory To Test For An ...
I recently wanted to unit test a method that required significant setup, and where an invalid method argument would throw an exception while ......
Read more >
Introduction - Fluent Assertions
Fluent Assertions supports a lot of different unit testing frameworks. Just add a reference to the corresponding test framework assembly to the unit...
Read more >
Exceptions/Catch an exception thrown in a nested call
Show how to create a user-defined exception and show how to catch an exception raised from several nested calls away.
Read more >
Verilog Recursion : r/FPGA
I have used Automatic keyword with the functions, have seen but not tried generate blocks, and attempted to invoke the recursive limit ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found