Entitity Framework 4.1 - Code First- Unit testing data access layer

后端 未结 1 1709
臣服心动
臣服心动 2021-01-28 10:35

I\'m a .NET developer and I\'m writing tests for my data access layer. I have tests that use fake repository - I have achieved that by using Moq and Ninject.

I\'m gettin

相关标签:
1条回答
  • 2021-01-28 10:51

    How do you expect to test data access if you don't access the data? Yes data access should be tested against real database. There is very simple workaround for your problem. Make your test transactional and rollback changes at the end of the test. You can use base class like this (NUnit):

    [TestFixture]
    public abstract class BaseTransactionalTest
    {
        private TransactionalScope _scope = null;
    
        [SetUp]
        public void Initialize()
        {
            _scope = new TransactionalScope(...);        
        }
    
        [TearDown]
        public void CleanUp()
        {
            if (_scope != null)
            {
                _scope.Dispose();
                _scope = null;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题