Entity Framework Unit Testing problem and solution

Source: Internet
Author: User

In fact, the solution mentioned below is not only applicable to projects where Ef is applied, but also for tests involving data access.

Do not post the original text link first:

(1) 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 (2)

(3) 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 usingTransactionScopeClass. 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 useTransactionScopeClass your project will need a referenceSystem.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 ownTransactionScopeWithout addingUsingBlock 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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.