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

大兔子大兔子 提交于 2019-12-02 08:01:31

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;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!