[ExpectedException(typeof (ArgumentNullException))]
public
void ConstructorTestNull()
{ User target = new User(null);
} [TestMethod()]
[ExpectedException(typeof(ArgumentException))]
public
void ConstructorTestEmpty()
{ User target = new User("");
} [TestMethod()]
[ExpectedException(typeof(ArgumentNullException))]
public
void ConstructorTestBlank()
{ User target = new User(" ");
}
如果不修改类库中的代码,单元测试会报告这三个新的测试都失败了。
小飞对代码做了相应的修改。结果出了这样的错误,见代码清单11-5:
代码清单11-5
Test method UserTest.UserTest.ConstructorTestBlank threw exception System.ArgumentException, but exception System. ArgumentNull- Exception was expected. Exception message: System.Argument- Exception: Value does not fall within the expected range.
大家定睛一看,原来小飞的Copy/Paste用了原来的ArgumentNullExcep- tion,而不是ArgumentException。
如果有人加了下面的代码: