如何声明抛出异常而不管消息?

时间:2016-02-24 16:52:21

标签: c# nunit nunit-3.0

我希望如果 serial 无法使用int.TryParse解析,那么它将抛出ArgumentException:

        if(!int.TryParse(serial,out intSerialNumber))
        {
            throw new ArgumentException("The serial number is not numeric.",nameof(serial));
        }

但是,当我测试此行为时,我的测试失败了:

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void throws_argumentException_if_serial_number_is_not_numeric()
    {
        var sut=new mDevice(serial:"something not numeric",patientid:123);
    }

我得到的例外是:

enter image description here

预期的异常确实被触发了,为什么测试没有通过?

班级代码:

public class mDevice
{
    public mDevice(string serial,int patientid)
    {
        int intSerialNumber;
        serial=serial.Trim();
        if(!int.TryParse(serial,out intSerialNumber))
        {
            throw new ArgumentException("The serial number is not numeric.",nameof(serial));
        }
        if(patientid<2)
        {
            throw new ArgumentException("The patient id must be greater than 1.",nameof(patientid));
        }
        Serial=serial;
        PatientId=patientid;
    }//more stuff after this constructor, but not including it here
}

0 个答案:

没有答案