Invoking should support Func<TInput, TResult>
See original GitHub issueDescription
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:
- Created 5 years ago
- Reactions:1
- Comments:10 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Alright, it seems that
Invoking
is the best option considering the various situations we need to cover.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
orLoad
might be more proper prefixes.Func<T, TResult>
can match a property, whereGetting
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.