Entity Framework Unit Testing problem and solution(转)
其实下文中说的解决方案不仅仅适用于应用了Ef的项目,其它涉及数据访问的测试同样适用。
不说先贴上原文链接 :
(一) 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).
作者:today4king
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2009-07-25 通过应用程序域AppDomain加载和卸载程序集(转自张逸)
2008-07-25 随机时间回调线程