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.

Throws() method does not work properly

See original GitHub issue

.Invoking() a method that clearly throws fails the .Throw() assertion. I also can’t step inside the method being invoked while debugging, assertion exception is just thrown.

My code:

public class MyClass
{
    public void Method()
    {
        throw new DirectoryNotFoundException();
    }
}

Test method:

    [Fact]
    public void TestMethod()
    {
        var sut = new MyClass();

        sut.Invoking(sut => sut.Method()).Should().Throw<FileNotFoundException>();
    }

Expected behavior:

The assertion should not fail.

Actual behavior:

It fails with the message:

Message: 
Expected a <DirectoryNotFoundException> to be thrown, but no exception was thrown.

Versions

  • Which version of Fluent Assertions are you using?
  • 6.6.0
  • Which .NET runtime and version are you targeting? E.g. .NET framework 4.6.1 or .NET Core 2.1.
  • .NET6

This is the second time this issue reoccurs. First time it happened I lost a lot of time trying to figure out why it doesn’t work, although everything is correct until quitted with frustration and lack of any results. It just didn’t work no matter what, I also tried to clear NuGet packages, restart VisualStudio, computer… Nothing helped. I haven’t opened that project for a week and when eventually did, it just worked without me touching anything.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
GualaBananacommented, May 18, 2022

Could you say how?

The problem was that I was using it to assert an exception that must be thrown from the method that returns IEnumerable and these require another type of assertion - .Enumerating().

From the official documentation on exceptions:

If the method you are testing returns an IEnumerable or IEnumerable<T> and it uses the yield keyword to construct that collection, just calling the method will not cause the effect you expected because the real work is not done until you actually iterate over that collection. You can use the Enumerating() extension method to force enumerating the collection like this.

Func<IEnumerable<char>> func = () => obj.SomeMethodThatUsesYield("blah");
func.Enumerating().Should().Throw<ArgumentException>();

You do have to use the Func<T> type instead of Action<T> then. Or you can do it like this:

obj.Enumerating(x => x.SomeMethodThatUsesYield("blah")).Should().Throw<ArgumentException>();

0reactions
MikDal002commented, May 18, 2022

Thank you, I missed that part of documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java - throws not working for a subtype of an exception that ...
So, I have my test method in which I'm just throwing an SSLHandshakeException which is a subtype of IOException . The output for...
Read more >
Throwing an exception when a method does not complete ...
Basically here is the situation: You have a function that is expected to return a result, but the function fails (parameter is bad,...
Read more >
How to use the Throws keyword in Java (and when ...
The throws keyword is used in a method signature and declares which exceptions can be thrown from a method. The throws keyword can...
Read more >
How to Throw Exceptions in Java
How to throw exceptions in Java ... Throwing an exception is as simple as using the "throw" statement. You then specify the Exception...
Read more >
throw - JavaScript - MDN Web Docs - Mozilla
The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be ...
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