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.

Unexpected behavior with IEnumerable and exceptions

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mu88commented, Nov 16, 2020

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.

0reactions
jnyrupcommented, Nov 16, 2020

@mu88 You’ve got it.

Read more comments on GitHub >

github_iconTop 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 >

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