0

我期望如果无法用 解析串行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);
    }

我得到的例外是:

在此处输入图像描述

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

类的代码:

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
}
4

0 回答 0