Transaction Question for automatic testing

2 posts, 0 answers
  1. Remco
    Remco avatar
    4 posts
    Member since:
    Oct 2013

    Posted 16 Apr 2014 Link to this post


    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:

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

    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?

  2. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 18 Apr 2014 Link to this post

    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. 

    Kaloyan Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. DevCraft banner
Back to Top