日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Entity Framework Unit Testing problem and solution(转)

發(fā)布時(shí)間:2025/3/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Entity Framework Unit Testing problem and solution(转) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

其實(shí)下文中說(shuō)的解決方案不僅僅適用于應(yīng)用了Ef的項(xiàng)目,其它涉及數(shù)據(jù)訪問(wèn)的測(cè)試同樣適用。

不說(shuō)先貼上原文鏈接 :

(一)?http://graemehill.ca/unit-testing-an-entity-framework-data-access-layer-part-1-just-hit-the-database

(二)http://graemehill.ca/unit-testing-an-entity-framework-data-access-layer-part-2-rolling-back-the-test-database

(三)?http://graemehill.ca/high-performance-database-rollback-in-automated-tests-with-sql-server

?

A couple months ago I wrote this article explaining why I think it is reasonable for unit tests to hit a real database. Subsequently, I wrote a follow up article describing some techniques for rolling back your database to its original state after each test. In that article I found that just using simple transactions did not solve the problem because you need access to all database connections being used, and they all have to be rolled back. I have since found a way around this problem using distributed transactions.

With the Microsoft Distributed Transaction Coordinator (MSDTC) the activity over multiple connections can be lumped into a single transaction using the TransactionScope class. MSDTC needs to be running for this to work, but since this is just for unit tests it doesn’t need to be enabled on your production environment.

In order to use the TransactionScope class your project will need a reference to System.Transactions. Here’s a sample unit test using MSTest and Entity Framework where the database is altered with multiple connections within a transaction and then the changes are rolled back:

Imports System.Transactions Imports System Imports System.Text Imports System.Collections.Generic Imports Microsoft.VisualStudio.TestTools.UnitTesting ? <TestClass()> _ Public Class UnitTestSample ? <TestMethod()> _ Public Sub ProofOfConceptTest() Using New TransactionScope Dim conn1 As New DataTestEntities Dim conn2 As New DataTestEntities ? Dim row1 As New Users With {.userName = "user1", .password = "pass"} Dim row2 As New Users With {.userName = "user2", .password = "pass"} ? conn1.AddToUsers(row1) conn2.AddToUsers(row2) ? conn1.SaveChanges() conn2.SaveChanges() ? Dim conn3 As New DataTestEntities Assert.AreEqual(conn3.Users.Count, 6) End Using End Sub ? End Class

Alternatively, if you want every test method inside a test class to be within its own TransactionScope without adding a Using block to every single test, you can use the initialization and cleanup methods like this:

Imports System.Transactions Imports System Imports System.Text Imports System.Collections.Generic Imports Microsoft.VisualStudio.TestTools.UnitTesting ? <TestClass()> _ Public Class UnitTestSample ? Private _transaction As TransactionScope ? <TestInitialize()> _ Public Sub Setup() _transaction = New TransactionScope End Sub ? <TestCleanup()> _ Public Sub TearDown() _transaction.Dispose() End Sub ? <TestMethod()> _ Public Sub ProofOfConceptTest() Dim conn1 As New DataTestEntities Dim conn2 As New DataTestEntities ? Dim row1 As New Users With {.userName = "user1", .password = "pass"} Dim row2 As New Users With {.userName = "user2", .password = "pass"} ? conn1.AddToUsers(row1) conn2.AddToUsers(row2) ? conn1.SaveChanges() conn2.SaveChanges() ? Dim conn3 As New DataTestEntities Assert.AreEqual(conn3.Users.Count, 6) End Sub ? End Class

As long as the use of MSDTC is an option, I have found this method to be far better than any of those described in the last article. It guarantees that the state or your database is maintained and is extremely fast (at least on small amounts of data).?

轉(zhuǎn)載于:https://www.cnblogs.com/jinzhao/archive/2011/07/25/High-performance-database-rollback-in-automated-tests-with-SQL-Server.html

總結(jié)

以上是生活随笔為你收集整理的Entity Framework Unit Testing problem and solution(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。