Unit Testing – Effort

Effort.EF – it is library to mock you EntityFramework database context and allow you test code without access to this ‘real’ database like MSSQL or Oracle.
Effort is installed together with EntityFramework and NMemory (in memory database).

Imagine you have a simple class with the only one method in it. This method counts people stored in database (Entity Framework is used).

    public class PeopleManager
    {
        public static int CountPeople(TSQL2012Entities entities)
        {
            return entities.ThePeople.Count();
        }
    }

You want to unit test this method, but without changing your real database. You can use Effort then.
Just create UnitTests project, install Effort.EF (using i.e. Nuget) add reference to your database project and project to be tested.
Now you can write like below.

        [TestMethod]
        public void PeopleManagerCountPeopleTransientTest()
        {
            //transient database context is created
            using (var context = new TSQL2012Entities(Effort.EntityConnectionFactory.CreateTransient("name=TSQL2012Entities")))
            {
                //entities are added
                context.ThePeople.Add(new ThePeople() { id = 0, firstname = "John", lastname = "Smith" });
                context.ThePeople.Add(new ThePeople() { id = 1, firstname = "Jane", lastname = "Brown" });
                
                //changes are stored into in memory database
                context.SaveChanges();

                //method is tested
                var peopleManagerCount = PeopleManager.CountPeople(context);
                Assert.AreEqual<int>(2, peopleManagerCount); //passed
            }
        }

That’s easy! There is only one thing to remember. When you create context using CreateTransient method, your in memory database is cleared always when you go outside the using clause. To avoid such behaviour you can use CreatePersisten method. See examples below.

        [TestMethod]
        public void PeopleManagerCountPeopleTransient2Test()
        {
            //transient database context is created, entites are added and stored
            using (var context = new TSQL2012Entities(Effort.EntityConnectionFactory.CreateTransient("name=TSQL2012Entities")))
            {
                context.ThePeople.Add(new ThePeople() { id = 0, firstname = "John", lastname = "Smith" });
                context.ThePeople.Add(new ThePeople() { id = 1, firstname = "Jane", lastname = "Brown" });
                context.SaveChanges();
            }

            //database context is cleared

            //method is tested, but database context is empty, so it fails
            using (var context = new TSQL2012Entities(Effort.EntityConnectionFactory.CreateTransient("name=TSQL2012Entities")))
            {
                var peopleManagerCount = PeopleManager.CountPeople(context);
                Assert.AreEqual<int>(2, peopleManagerCount);
            }
        }


        [TestMethod]
        public void PeopleManagerCountPeopleOnePersistentTest()
        {
            //persistent database context is created, entites are added and stored
            using (var context = new TSQL2012Entities(Effort.EntityConnectionFactory.CreatePersistent("connectionA", "name=TSQL2012Entities")))
            {
                context.ThePeople.Add(new ThePeople() { id = 0, firstname = "John", lastname = "Smith" });
                context.ThePeople.Add(new ThePeople() { id = 1, firstname = "Jane", lastname = "Brown" });
                context.SaveChanges();
            }

            //data is not cleared

            using (var context = new TSQL2012Entities(Effort.EntityConnectionFactory.CreatePersistent("connectionA", "name=TSQL2012Entities")))
            {
                //method is tested and test passes
                var peopleManagerCount = PeopleManager.CountPeople(context);
                Assert.AreEqual<int>(2, peopleManagerCount);
            }
        }

More information: https://effort.codeplex.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s