Unexpected behavior with IEnumerable and exceptions
See original GitHub issueDescription
When calling .Should().Throw()
on a method that uses LINQ and returns an IEnumerable
, the enumeration won’t get executed. This can hide exceptions that occur within a LINQ statement.
Complete minimal example reproducing the issue
The following code needs MSTest. It is a rather simple example of some code I have in front of me, but hopefully, the problem gets clear: I have to call .ToList()
to actually execute the enumeration and get the expected error.
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void ThisShouldThrow()
{
var testee = new Example();
testee.Invoking(x => x.DoSomething()).Should().Throw<InvalidOperationException>();
}
[TestMethod]
public void ThisThrows()
{
var testee = new Example();
testee.Invoking(x => x.DoSomething().ToList()).Should().Throw<InvalidOperationException>();
}
}
public class Example
{
public IEnumerable<int> DoSomething()
{
var range = Enumerable.Range(0, 10);
return range.Select(Twice);
}
private int Twice(int arg)
{
if (arg == 5)
{
throw new InvalidOperationException();
}
return 2 * arg;
}
}
}
Expected behavior:
I would expect that all exception handling methods (e. g. Throw()
, NotThrow()
, etc.) actually enumerate the underlying collection (e. g. by calling .ToList()
). Otherwise this could lead to False Negatives.
Actual behavior:
The enumeration has to be done manually. If it gets forgotten, exceptions will be swallowed.
Versions
- Which version of Fluent Assertions are you using? 5.10.3
- Which .NET runtime and version are you targeting? .NET Framework 4.8
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
LINQ unexpected behavior when returning IEnumerable ...
The call to ToArray() causes a full enumeration of the IEnumerable and thus, the exception to occur. The second method does not enumerate...
Read more >Handling Null Values and Exceptions in C# LINQ Queries
In this article, we'll explore various techniques to handle null values and exceptions in C# LINQ queries. We'll discuss how to avoid common ......
Read more >Task.WhenAll - Inner exceptions are lost · Issue #31494
Hi,. Summary: when await Task.WhenAll(tasks) if more than one task fails, only one exception is thrown and I believe an AggregateException ...
Read more >InvalidOperationException Class (System)
InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments.
Read more >ASP.NET Core Best Practices
Do throw or catch exceptions for unusual or unexpected conditions. App diagnostic tools, such as Application Insights, can help to identify ...
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
Thanks for your explanation! That’s perfectly fine for me - I like your clear API philosophy 🙂
If it’s okay for you, you can assign this issue to me and I go for this PR.
@mu88 You’ve got it.