I have a class that creates a file.
I am now doing integration tests to make sure the class is ok.
I am passing in invalid directory and file names to make sure exceptions are thrown.
In my tests are I am using:
[Test] public void CreateFile_InvalidFileName_ThrowsException() { //Arrange var logger = SetupLogger("?\\"); //Act //Assert Assert.Throws<Exception>(()=> logger.CreateFile()); }
However in this scenario the test is failing as an ArgumentException is thrown. I thought by adding just Exception it would pass.
Is there a way to make this pass just using Exception?
NUnit is an open-source unit testing framework for the . NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.
Just like other classes can interact only with the public methods of your class, the same should apply to a unit test. The unit test should only test the public interface. Each private method is an implementation detail. They contain the logic necessary to implement a piece of functionality.
ASP.NET Core has an extremely useful integration testing framework, in the form of the NuGet package Microsoft.
The help for Assert.Throws<>
states that it "Verifies a delegate throws a particular type of exception when called"
Try the Assert.That
version as it will catch any Exception
:
private class Thrower { public void ThrowAE() { throw new ArgumentException(); } } [Test] public void ThrowETest() { var t = new Thrower(); Assert.That(() => t.ThrowAE(), Throws.Exception); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With