我希望如果 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);
}
我得到的例外是:
预期的异常确实被触发了,为什么测试没有通过?
班级代码:
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
}