This is a migrated thread and some comments may be shown as answers.

Transaction Question for automatic testing

1 Answer 33 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Remco
Top achievements
Rank 1
Remco asked on 16 Apr 2014, 12:28 PM
Hello,

In my automatic testing i want to be able to do several DB operations and after the test i like to revert the database to the original state.

Before with Entity Framework i was do  it in this way:



 [TestInitialize]
        public void TestInitialize()
        {
            transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew, new TimeSpan(0, 10, 0)); // original 5 min..
       ....
}


[TestCleanup]
public void TestCleanup()
{
     transactionScope.Dispose(); // explicit controlled rollback! 


so before every test it automatic start a new transactionscope, during testing i can do a lot of saving / manipulating of the Database and check if the results are as expected and on the end it will do the rollback off the database changes so that i am on the initial state again.


With Telerik DataAcess i dont know how i can do the same..  can you help me?








1 Answer, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 18 Apr 2014, 02:51 PM
Hello Remco,

Unfortunately the approach with the TransactionScope is not yet supported. I would suggest you one of the following alternatives:

1. You could use the OpenAccessContext instance itself as a transaction boundary. It can be achieved if you perform context.FlushChanges() instead of context.SaveChanges(). This call persists all changes but keeps the transaction open until an explicit call to SaveChanges() or ClearChanges(). You should ensure that all of the components work with a single instance of the context and inject is a dependency. At the end of the tests all you need to do is to call ClearChanges() which will rollback the transaction. If all of the operations are in the unit tests probably you can use this approach, it would be a bit more complicated if you have calls to SaveChanges() deeper in your layers as it will require a lot of refactoring.

2. You could have a clean-up code which to restore the DB to its initial state by executing some CUD operations.

3. You can execute your tests against a local (in memory) database like SQL Compact Edition. In such case you should have a prepared DB file with the required initial state and just copy a fresh file before each of the tests.

I hope this helps. 

Regards,
Kaloyan Nikolov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
General Discussions
Asked by
Remco
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Share this question
or