我正在进行单元测试。 我想使用ExpectedExceptionAttribute。单元测试时应该在ExpectedExceptionAttribute中使用哪种类型的异常?
我有一个雇员类,其中包含我已使用索引的用户名属性,所以用户名应该是唯一的。
代码如下。
public class EmployeeConfigration : EntityTypeConfiguration<Employee>
{
public EmployeeConfigration()
{
this.Property(x => x.FirstName).IsRequired().HasMaxLength(50);
this.Property(t => t.UserName).HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_UserName", 1) { IsUnique = true }));
}
}
现在,考虑下面的单元测试代码。
[TestMethod]
[ExpectedException(typeof(System.Data.SqlClient.SqlException), "Username duplication is not allowded")]
public void Insert_EmployeeWithSameUsername_Fails()
{
...
...
...
}
我用SQLEXCEPTION,但其没有工作,但它仍然抛出一个错误......我应该在单元测试代码中使用哪种异常?
如果您在调试器下运行它,那么抛出的异常的实际类型是什么? – 2014-10-28 11:29:10
我在调试模式下运行,它会抛出sqlException – bnil 2014-10-28 11:31:01
你确定它不是'DbUpdateException'吗? – RobH 2014-10-28 11:35:09