C
C#6mo ago
NeotericSh

Extending the Assert class in xunit

I have a method that validates user input and throws an exception if something went wrong. And thought I would extend the Assert class in xunit with a method DoesNotThrow to unit test it. I'm having some problem extending that class and tho that I could ask for help.
6 Replies
x0rld
x0rld6mo ago
you can use some lib like FluentAssertion for that https://fluentassertions.com/exceptions/
Fluent Assertions
Exceptions
canton7
canton76mo ago
Stack Overflow
Extending XUnit Assert class with new asserts
I'm trying to extend the xUnit assert method by adding some selenium functionality namespace MyProject.Web.Specs.PageLibrary.Extensions { public static class AssertExtensions { pub...
NeotericSh
NeotericShOP6mo ago
After some more exploring, I was unable to create an extension on the Assert class. And it just boils down to that it is a class that just have static methods in it. Looked at the source for xunit and ended up writing a wrapper class instead in the same style. Maybe you guys already know this, but if I mark a method with [Fact] and then throw an exception it counts as a bad test case, and if nothings happens it counts as a good test case. Thought I would share that. This is what I ended up with: namespace Tests.Extensions; public class Assert { public static void DoesNotThrow(Action action) { try { action(); } catch (Exception e) { throw new Exception($"Expected no exception, but got: {e.Message}"); } } }
canton7
canton76mo ago
Did you read my link? There's a base class you can derive your test class from, which has an Assert property, that you can add extension methods to
Unknown User
Unknown User6mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX6mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server