如何测试使用DbEntityValidationException的Exception扩展方法?

时间:2013-11-23 01:55:57

标签: c# entity-framework

我希望我只是在寻找一些非常简单的东西......

我需要的答案必须提供一个使用Moq的测试,因为我无法访问个人MSDN(专业)的Fakes。只有premium和ultimate才能使用Fakes。单元测试必须能够在不访问任何外部资源的情况下运行。没有使用Entity Framework DbContext连接到SQL数据库。

public static string ToDetailedString(this Exception ex)
{
    var builder = new StringBuilder();
    AddExceptionToBuilder(builder, ex);
    return builder.ToString();
}

private static void AddExceptionToBuilder(StringBuilder builder, Exception ex)
{
    builder.Append(builder.Length > 0 ? "\r\n" + ex.Message : ex.Message);

    var validationException = ex as DbEntityValidationException;
    if (validationException != null)
    {
        foreach (var detail in validationException.EntityValidationErrors.SelectMany(error => error.ValidationErrors))
        {
            builder.Append("\r\n" + detail.PropertyName + " : " + detail.ErrorMessage);
        }
    }

    if (ex.InnerException != null)
    {
        AddExceptionToBuilder(builder, ex.InnerException);
    }
}

这就是为什么测试这么难的原因。它不像创建新的DbEntityValidationException那么简单,因为你必须传入一个DbEntityValidationResult列表。如果不提供DbEntityEntry,则无法实例化DbEntityValidationResult。我发现创建DbEntityEntry的唯一方法是从DbContext.Entry()获取它。这意味着您必须拥有需要访问数据库的有效DbContext。

这是我能够完成工作的唯一代码,但需要访问数据库。

/// <summary>
/// This test method must be ran against the database. I cannot figure out a way to create an entry without
/// a valid context to a real database?
/// </summary>
[TestMethod]
public void ToDetailStringForDbEntityValiditionException()
{
    if (!TestHelper.RunUnitTestAgainstDatabase)
    {
        return;
    }

    var errors = new List<DbValidationError>();
    errors.Add(new DbValidationError("Username", Constants.UsernameLengthError));

    var context = new EntityFrameworkRepository();
    var entry = context.Entry(new User());
    var results = new List<DbEntityValidationResult>();
    var result = new DbEntityValidationResult(entry, errors);
    results.Add(result);

    var exception = new DbEntityValidationException("Registration Failed.", results);

    var expected = "Registration Failed.\r\nUsername : " + Constants.UsernameLengthError;
    var actual = exception.ToDetailedString();

    Assert.AreEqual(expected, actual);
}

1 个答案:

答案 0 :(得分:0)

这对我有用,使用我已经设置的实体模型和连接:

var context = new DbContext("name = JobManagerConnection");
var entry = context.Entry(new Models.JobManager.Job());
var errInner = new System.Data.Entity.Validation.DbValidationError("Test Property", "Test Message");
var lstErrInner = new List<System.Data.Entity.Validation.DbValidationError> { errInner };
var errOuter = new System.Data.Entity.Validation.DbEntityValidationResult (entry, lstErrInner);
var lstErrOuter = new List<System.Data.Entity.Validation.DbEntityValidationResult> { errOuter };
throw new System.Data.Entity.Validation.DbEntityValidationException("Test Data Exception", lstErrOuter);