0

Can you add messages to x.Should().Throw<MyException>() from fluent Assertion if the Test fails? In my scenario, I exactly know why this test would fail, and I want to display a message if it does, but I cant find a way. The closest thing I found was the because argument in the Throw(), but the text does not makes sense as it would be "Expected a < MyException > to be thrown because MY_REASON_THE_TEST_FAILED, but no exception was thrown."

If possible, I would like the failing message to be something like "The test failed, probably because MY_REASON_THE_TEST_FAILED"

  • Does this answer your question? [Assert an Exception using XUnit](https://stackoverflow.com/questions/45017295/assert-an-exception-using-xunit) (using `Assert.Throw`) – Pac0 Jul 22 '21 at 11:32
  • Is that `.Should()` from fluent assertions? Provide more details to clarify the actual problem. As written currently there is not enough information for us to be able to provide assistance. – Nkosi Jul 22 '21 at 12:19
  • 1
    Note that `Should().Throw` will pass if the exception is thrown and fail if it *(the expected exception)* was not thrown. the `because` would be only displayed if the exception was not thrown in that case. – Nkosi Jul 22 '21 at 12:22
  • @Pac0 No, it does not. It talks about the assertion of the thrown Exception. In my case, I want to display a specific message if no exception was thrown. – user16313138 Jul 22 '21 at 12:33
  • @Nkosi Yes, it is from fluen assertions. Sorry that I did not mention that. And yes, I know that it would pass the exception and that `because` would be displayed if no exception was thrown. And that is exactly what I want, but the pre-defined text of the `because` causes my explanation as to why no exception was thrown to be in the middle of another sentence which is not what i want. – user16313138 Jul 22 '21 at 12:34
  • [edit] the question with those details and a coed sample for us to use to reproduce the problem. Include an example of an actual message you receive. – Nkosi Jul 22 '21 at 12:38
  • 2
    That's the message format used by all FluentAssertion exceptions ("Expected Y [because R] but X"). Before continuing, ask yourself how much useful information is contained _in the string attached to an already-failed test_ when you include boilerplate like "the test failed". Your test runner will already report a failure; FA's failure messages default to telling you only _what_ failed, with the ability to add a contextual note about "why this is important". If you're still convinced you need a different format, try giving a clearer example so that it makes sense to others. – Tullo_x86 Jul 22 '21 at 14:21
  • @Nkosi All details I said in my comments, are already in the Post. All I told you are the same information but differently worded. – user16313138 Jul 23 '21 at 05:44
  • @Tullo_x86 My test is written in a way, that (as long as no one touches the test itself) there is only one way it would fail. But since the test is a bit bigger it tests about 28 different variable combinations (which are not seperated by `InlineData` since they are gathered via reflection) and I want to display which combination exactly failed and an information as to why it failed (like I said, only one reason for it to fail). – user16313138 Jul 23 '21 at 05:48
  • You can also wrap your assertion with an `AssertionScope` like [this test](https://github.com/fluentassertions/fluentassertions/blob/2e7a8782c3eebc99bd38ad37000b4eff6a87bd0d/Tests/FluentAssertions.Specs/Execution/AssertionScopeSpecs.cs#L335) is doing. – Dennis Doomen Jul 23 '21 at 18:11
  • @user16313138 In that case, your best options are probably to either embed the message into the test names, or to use xUnit's `[Theory]` tests instead of using `[Fact]` tests (although this second option only makes sense if the test cases only vary by one argument or combination of arguments) – Tullo_x86 Jul 23 '21 at 22:08

0 Answers0