Unit testing Entity Framework ObjectContext in ASP.NET application

I create an Objectity Object Entity Framework for each 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 as follows:

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

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

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

UPDATE:

I can create the IObjectContextPerRequest and ObjectContextPerRequest interface, 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()); 

        }
    }
+2
source share
1 answer

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

public class Repository
{
    private ObjectContext _ctx;

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

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

, IOC, .

:

[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()); 
    }
}
+2

All Articles