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:
- Created a year ago
- Comments:5
Top 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 >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
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:
obj.Enumerating(x => x.SomeMethodThatUsesYield("blah")).Should().Throw<ArgumentException>();
Thank you, I missed that part of documentation.