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.

Should().Throw does not catch the expected exception

See original GitHub issue

Hey,

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

github_iconTop GitHub Comments

2reactions
eNeRGy164commented, Aug 19, 2021

I think what you want to do is:

[TestMethod]
public void Calculate_Null_Throws()
{
    //given
    byte[] array = null;

    //when
    Action act = () => array.Calculate();

    act.Should().Throw<ArgumentNullException>(); // then
}
1reaction
dennisdoomencommented, Aug 19, 2021

hmm … I still wonder why the catch does not work, due to the fact that the null check from Invoke() is throwing an ArgumentNullException and not the .NET Framework.

Because Invoking doesn’t do much other than build a Func that will invoke your code and pass it to Should().Throw. But in v6, before we build that func, we first assert you didn’t pass null.

Read more comments on GitHub >

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

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