-
Notifications
You must be signed in to change notification settings - Fork 731
Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels