ASP.NET应用程序中的单元测试实体框架ObjectContext [英] Unit Testing Entity Framework ObjectContext in ASP.NET Application

查看:77
本文介绍了ASP.NET应用程序中的单元测试实体框架ObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码为每个ASP.NET请求创建Entity Framework ObjectContext:

I am creating the Entity Framework ObjectContext per ASP.NET Request using the following code:

 public static class ObjectContextPerRequest
    {
        public static EStudyTestDatabaseEntities Context
        {
            get
            {
                var _context = HttpContext.Current.Items["EStudyModel"] as EStudyTestDatabaseEntities; 

                if(_context == null)
                {
                    _context = new EStudyTestDatabaseEntities(); 
                    HttpContext.Current.Items.Add("EStudyModel", _context);
                }

                return _context; 
            }
        }

        public static void RemoveContext()
        {
            var _context = HttpContext.Current.Items["EStudyModel"] as EStudyTestDatabaseEntities; 

            if(_context != null)
            {
                _context.Dispose();
            }
        }
    }

我在存储库中使用它像这样:

In the Repository I use it like this:

public class RoleRepository : IRoleRepository
    {
        public IList<Role> GetAll()
        {
            using(var db = ObjectContextPerRequest.Context)
            {
                return db.RoleSet.ToList(); 
            }
        }
    }

如果我跑步,效果很好应用程序。问题在于我将如何对存储库进行单元测试,因为我需要创建HttpContext。

This works fine if I run the application. The problem is how I will unit test the Repository because I need to create a HttpContext.

  [TestFixture]
    public class when_getting_all_roles
    {
        [Test]
        public void should_get_roles_successfully()
        {
            var repository = new RoleRepository();
            Assert.AreNotEqual(4,repository.GetAll()); 
        }
    }

更新:

我可以使IObjectContextPerRequest接口和ObjectContextPerRequest如下所示:

I can make IObjectContextPerRequest interface and ObjectContextPerRequest as shown below:

 public interface IObjectContextPerRequest
    {
        EStudyTestDatabaseEntities Context { get; }
        void RemoveContext(); 
    }

现在我可以按如下方式编写测试:

And now I can write my test as follows:

[TestFixture]
    public class when_getting_all_roles
    {
        [Test]
        public void should_get_roles_successfully()
        {
            var objectContextPerRequestStub = MockRepository.GenerateStub<IObjectContextPerRequest>();

            objectContextPerRequestStub.Expect(x => x.Context).Return(new EStudyTestDatabaseEntities()); 

            var repository = new RoleRepository(objectContextPerRequestStub);
            Assert.AreNotEqual(4,repository.GetAll()); 

        }
    }


推荐答案

您可以定义两个存储库构造函数,并在测试中使用一个,在应用程序中使用第二个:

You can define two repository constructors and use one in tests, second in application:

public class Repository
{
    private ObjectContext _ctx;

    public Repository()
    {
        _ctx = ObjectContextPerRequest.Context;
    }

    public Repository(ObjectContext ctx)
    {
        _ctx = ctx;
    }
}

如果使用IOC容器,则只能定义一个构造函数

You could define only one constructor if you used IOC container, but that is much more to explain.

使用构造函数,测试将更容易编写:

With constructors tests will be easier to write:

[TestFixture]
public class when_getting_all_roles
{
    [Test]
    public void should_get_roles_successfully()
    {
        var repository = new RoleRepository(new EStudyTestDatabaseEntities());
        Assert.AreNotEqual(4,repository.GetAll()); 
    }
}

这篇关于ASP.NET应用程序中的单元测试实体框架ObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆