With # Xunit, Assert.Throws fails if expected exception is thrown

EDIT: There is a similar question here, but the solutions only offer workarounds and do not give any information about the cause of the problem or how to fix it. This question may still be a duplicate.

EDIT 2: It turns out that this problem only occurs during debugging, although this has not happened before. After replacing (TCheck)nullwith null as TChecktests, they pass at startup, but they throw an exception during debugging.

ORIGINAL MAIL: I have a method in unit test that looks like

internal void EqualityAssert<TCheck, TEquatable>(TEquatable item, ... ) 
    where TCheck : class, IEquatable<TEquatable>, TEquatable
{

    // Various equality assertions that are passing
    // ...

    // A == null       
    Assert.Throws<NullReferenceException>(
    () => ((IEquatable<TEquatable>)item).Equals((TCheck)null));            
}

, , " NullReferenceException " , .

Assert.Throws , , , .

+4
2

:

[Fact]
public void Divide_TwoNumbers_ExpectException()
{
    var sut = new Calculator();
    var exception = Record.Exception(() => sut.Divide(10, 0));
    Assert.IsType(typeof(DivideByZeroException), exception);
}
+4

, . . .

var message = FakeRequestBuilder.CreateSettlementFileMessage();

var warning = Assert.Throws<ExF.Core.Exception.IntegrationValidationException>(
                () => createSettlementFileHandler.Handle(message));

Assert.Equal(warning.ErrorCode, -1);
0

All Articles