单元测试实体框架验证

单元测试实体框架验证

本文介绍了单元测试实体框架验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为使用IValidatableObject的一些Entity Framework对象编写了一些自定义验证,并且已经添加了一些DataAnnotations给对象进行验证。



我想测试验证是满足所需的验证(确保自定义验证工作,所做的任何更改都保留了这些数据注释等),但是我无法确定如何在单元测试中运行验证,而不调用SaveChanges(我不想做一个事情,如果有一个问题,验证不会工作,它会写入数据源)



我想做这样的事情:

  [TestMethod] 
public void InvalidStartDate_StartDateAfterEndDate()
{
var header = new Header()
{
StartDate = DateTime.Now.AddDays(15),
EndDate = DateTime.Now.AddDays(-15)
};
var actual = header.IsValid();
var expected = false;
Assert.AreEqual(expected,actual);
}

或类似于

  [TestMethod] 
public void InvalidStartDate_StartDateAfterEndDate()
{
var header = new Header()
{
StartDate = DateTime.Now.AddDays(15),
EndDate = DateTime.Now.AddDays(-15)
};
var actual = header.GetValidationErrors()。Count;
var expected = 0;
Assert.AreEqual(expected,actual);
}

但似乎找不到一种让验证运行没有调用保存更改,是否有办法?

解决方案

您可以调用验证该对象。

  [TestMethod] 
public void InvalidStartDate_StartDateAfterEndDate()
{
var header = new Header()
{
StartDate = DateTime .Now.AddDays(15),
EndDate = DateTime.Now.AddDays(-15)
};

var context = new ValidationContext(header,null,null);
var results = new List< ValidationResult>();

var actual = Validator.TryValidateObject(header,context,results);
var expected = false;

Assert.AreEqual(expected,actual);
}


I have written some custom validation for some Entity Framework objects using IValidatableObject and I have added some DataAnnotations to the objects for validation.

I wanted to test that validation is meeting the required validation (ensuring that the custom validation is working and that any changes made keep those Data Annotations etc...) but I can't determine how to run the validation in the unit test without calling SaveChanges (which I don't want to do as if there is an issue and validation doesn't work it would write to the data source)

I wanted to do something like this:

[TestMethod]
public void InvalidStartDate_StartDateAfterEndDate()
{
   var header = new Header()
                    {
                        StartDate = DateTime.Now.AddDays(15),
                        EndDate = DateTime.Now.AddDays(-15)
                    };
   var actual = header.IsValid();
   var expected = false;
   Assert.AreEqual(expected, actual);
}

Or something like

[TestMethod]
public void InvalidStartDate_StartDateAfterEndDate()
{
   var header = new Header()
                    {
                        StartDate = DateTime.Now.AddDays(15),
                        EndDate = DateTime.Now.AddDays(-15)
                    };
   var actual = header.GetValidationErrors().Count;
   var expected = 0;
   Assert.AreEqual(expected, actual);
}

But can't seem to find a way of getting the validation to run without calling save changes, is there a way to do this?

解决方案

You can invoke the Validator to validate the object.

[TestMethod]
public void InvalidStartDate_StartDateAfterEndDate()
{
   var header = new Header()
                    {
                        StartDate = DateTime.Now.AddDays(15),
                        EndDate = DateTime.Now.AddDays(-15)
                    };

   var context = new ValidationContext(header, null, null);
   var results = new List<ValidationResult>();

   var actual = Validator.TryValidateObject(header, context, results);
   var expected = false;

   Assert.AreEqual(expected, actual);
}

这篇关于单元测试实体框架验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:02