Should().Throw does not catch the expected exception
See original GitHub issueHey,
I upgraded the .NET Framework to 4.8 (previous 4.7.1) and the Nuget to v6.0.0 (previous v5.10.2), but now I’m encountering some strange behavior in one of my UnitTests:
[TestMethod]
public void Calculate_Null_Throws()
{
//given
byte[] array = null;
//when
array.Invoking(foo => foo.Calculate())
.Should().Throw<ArgumentNullException>(); // then
}
The ArgumentNullException
will be thrown like expected, but the .Should().Throw<ArgumentNullException>()
does not catch the exception. This UnitTest already worked with version 5.10.2 (with the .NET Framework 4.7.1).
Exception Message:
Test method FluentAssertionsTests.FluentAssertionsTest.SubArray_Null_Throws threw exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: subject
at FluentAssertions.AssertionExtensions.Invoking[T,TResult](T subject, Func`2 action)
Versions
.NET Framework 4.8 FluentAssertions 6.0.0
Additional Information
Created a short solution to test the behavior: FluentAssertionsTests.zip
Keep up your great work!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Assert.Throws method doesn't catch the expected exception
Your method is an enumerable built using yield return . What's tricky about it is that nothing will actually happen unless you enumerate...
Read more >Assert an Exception Is Thrown in JUnit 4 and 5
In this quick tutorial, we'll be looking at how to test if an exception was thrown using the JUnit library. We will, of...
Read more >Unit Tests for Expected Exceptions
Call the code that should be throwing an exception. Assert that an exception was thrown, and it was the expected type of exception....
Read more >Testing for Expected Exceptions in JUnit 5: Quite an ...
We see from the documentation that assertThrows() returns a Throwable (if the assertion does not fail), which we can save to a local...
Read more >JUnit Test Exception Examples - How to assert an ...
As you can see, we use the fail() statement at the end of the catch block so if the code doesn't throw any...
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
I think what you want to do is:
Because
Invoking
doesn’t do much other than build aFunc
that will invoke your code and pass it toShould().Throw
. But in v6, before we build that func, we first assert you didn’t passnull
.