Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Testing with a Mock database #1270


User avatar
0
acrigney created

Hi Guys, Love your work! But I need to have a mock implementation of the database for the application just like you can with the base unit tests classes. I tested this out with your SimpleTaskSystem sample project.

I was wondering what you thought of this approach. I created a new project called SimpleTaskSystem.InMemoryData with a module SimpleTaskSystemInMemoryDataModule like this

using System.Reflection; using Abp.EntityFramework; using Abp.Modules; using Castle.MicroKernel.Registration; using System.Data.Common; using System; using SimpleTaskSystem.InMemoryData; using SimpleTaskSystem.EntityFramework;

namespace SimpleTaskSystem { [DependsOn(typeof(SimpleTaskSystemCoreModule), typeof(AbpEntityFrameworkModule))] public class SimpleTaskSystemInMemoryDataModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

        IocManager.IocContainer.Register(
            Component.For<SimpleTaskSystemDbContext>()
                .Instance(SimpleTaskSystemInitialDataBuilder.Build(new SimpleTaskSystemDbContext(Effort.DbConnectionFactory.CreatePersistent("1"))))
                .IsDefault().Named("InMemoryData")                          
                .LifestyleSingleton()
            );
}

}

With a module class like this using System.Data.Entity.Migrations; using System.Linq; using SimpleTaskSystem.EntityFramework; using SimpleTaskSystem.People; using SimpleTaskSystem.Tasks;

namespace SimpleTaskSystem.InMemoryData { public static class SimpleTaskSystemInitialDataBuilder { public static SimpleTaskSystemDbContext Build(SimpleTaskSystemDbContext context) { //Add some people

        context.People.AddOrUpdate(
            p => p.Name,
            new Person {Name = "Isaac Asimov"},
            new Person {Name = "Thomas More"},
            new Person {Name = "George Orwell"},
            new Person {Name = "Douglas Adams"}
            );

        context.SaveChanges();

        //Add some tasks

        context.Tasks.AddOrUpdate(
            t => t.Description,
            new Task
            {
                Description = "my initial task 1"
            },
            new Task
            {
                Description = "my initial task 2",
                State = TaskState.Completed
            },
            new Task
            {
                Description = "my initial task 3",
                AssignedPerson = context.People.Single(p => p.Name == "Douglas Adams")
            },
            new Task
            {
                Description = "my initial task 4",
                AssignedPerson = context.People.Single(p => p.Name == "Isaac Asimov"),
                State = TaskState.Completed
            });

        context.SaveChanges();
        return (context);
    }
}

} And I just reference Effort and NMemory in the application. And change the data module to this module in the application. However when I want to switch back to testing with the Mock data at run time I would like to set the data dependent module at run time, so I can easily switch between the two if I have to. So how can I do a dynamic dependency?

Best Regards, Alistair


1 Answer(s)
  • User Avatar
    0
    acrigney created

    Perhaps I could do an upgrade to EF7 it has support for InMemory databases with a nuget package and allows configuration of which type to use.