English 中文(简体)
如何对实体框架法进行检验?
原标题:How can I unit test Entity Framework Code First Mappings?

I m using Code First to map classes to an existing database. I need a way to unit test these mappings, which are a mix of convention-based, attribute-based, and fluent-api.

为了进行单位测试,我需要确认,各班图的特性符合数据库正确表格和栏目。 这一测试需要根据具体情况进行,并应首先涵盖所有配置选择。

At a very high level, I d be looking to assert something like (pseudo-code):

Assert.IsTrue(context.TableFor<Widget>().IsNamed("tbl_Widget"));
Assert.IsTrue(context.ColumnFor<Widget>(w => w.Property).IsNamed("WidgetProperty"));
问题回答

另一个考虑的想法是使用Linq和ToString()。

eaxample:

context.Widget.Select(c => c.Property).ToString()

这将使服务器提供商能够做到这一点:

"SELECT [Var_3].[WidgetProperty] AS [WidgetProperty] FROM [dbo].[Widget]..."

现在,我们可以在某种推广方法中掩盖这一点。

Assert.IsTrue(context.Widgets.GetSqlColumnNameFor(w => w.Property).IsNamed("WidgetProperty"));

Draft for extension :

public string GetSqlColumnNameFor<TSource>(this DbSet<T> source, Expression<Func<TSource, TResult>> selector)
{
    var sql = source.Select(selector).ToString();

    var columnName = sql... // TODO : Some regex parsing

    return 
       columnName;
}

同样,我们也可以创建GetSqlTableNameFor()。

UPDATE :我决定寻找一些智囊团,因此,这一解决办法更为笼统,显然有这样的事情。 NET:

http://www.dpriver.com/blog/list-of-demos-illustrate-how-how-to-use-general-sql-parser/generate-internal-query-par-in-chloro-fur-m-ylfor-

我认为,涵盖所有可能选择的唯一途径是利用实体框架权力工具预先整理你们的DbContext的意见,并可能利用对生成的那种类型的综合思考,并对生成的代码本身进行检索,以核实你想要它的所有地图。 对我来说,声音非常痛苦。

Another thing that comes to mind is creating a facade around DbModelBuilder to intercept and check everything that passes through it, but I don t know if that would handle the convention-based stuff. Also sounds painful.

As a less-complete, but much easier alternative, you can probably knock out a large portion of this by switching to attribute-based mapping wherever possible. This would allow you to create a base test class, say, ModelTesting<TEntity>, which includes a few test methods that use reflection to verify that TEntity has:

  • A single TableAttribute.
  • Each property has a single ColumnAttribute or NotMappedAttribute.
  • At least one property with a KeyAttribute.
  • Each property type maps to a compatible database type.

你甚至可以执行根据财产和类别名称命名的命名公约(表层层层层)。 还可以检查外国的关键绘图。 这是你可以一劳永逸地从你们的每一种模式中得出的一个笔记本基级,可以追捕你们的大多数错误(同样,大多数的地雷,不管怎样的)。

Anything that can t be represented by attributes, like TPH inheritance and such, becomes a little harder. An integration test that fires up the DbContext and does a FirstOrDefault on Set<TEntity>() would probably cover most of those bases, assuming your DbContext isn t generating your database for you.

如果你写了一种方法的话,那就是一种方法。

public static string ToMappingString(this Widget obj)

然后,你可以通过批准测试(www.approvaltests.com或nuget)轻易测试。

There s a video here: http://www.youtube.com/watch?v=vKLUycNLhgc

However, if you are looking to test "My objects save and retrive themselves" Then this is a perfect place of "Theory Based Testing"

Theory based testing Most unit test take the form of

Given A,B expect C

理论测试

Given A,B expect Theory

无需担心哪一种特殊形式的A & B就算,因为你不需要知道C,所以任何随机发电机都会工作。

例1:测试添加和分流方法

正常情况下,你会遇到同样的问题。

Assert.AreEqual(5, Add(2,3));
Assert.AreEqual(9, Add(10,-1));
Assert.AreEqual(10, Add(5,5));
Assert.AreEqual(7, Subtract(10,3));

但是,如果你写了字句的话,你就写了字。 它希望测试。

for(int i = 1; i < 100; i++)
{ 
    int a = random.Next();
    int b = random.Next();
    Assert.AreEqual(a, Subtract(Add(a,b),b, string.Format("Failed for [a,b] = [{0},{1}], a,b));        
}

现在你们理解 理论检验,你试图测试的理论

Given Model A
When A is stored to the database, and retrieved the resulting object is equal to A




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签