Saturday, March 18, 2017

Entity Framework Code First Unit/Integration Testing Using Effort

We can unit test Entity Framework Code First using Effort.
Effort can be installed in test project using nuget package https://www.nuget.org/packages/Effort.EF6/ 

Following below steps we can have basic EF code first unit test.

Create Student Domain Class

 
 public class Student
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public int CollegeId { get; set; }
        public DateTime DOB { get; set; }

    }



Create Mapping Class

 
 public class StudentMap : EntityTypeConfiguration
    {
        public StudentMap()
        {

            this.ToTable("Student");

            this.HasKey(s => s.Id);

            this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            this.Property(s => s.Email).IsRequired().HasMaxLength(50);

            this.Property(s => s.FirstName).IsRequired().HasMaxLength(50);

            this.Property(s => s.LastName).IsRequired().HasMaxLength(50);

            this.Property(s => s.DOB).IsRequired();
          
        }
    }



Create School Context Class

Note: There is DatabaseInitializer which is drop and create always required for unit testing.
 
 public class SchoolContext : DbContext
    {
        public SchoolContext() : base()
        {
            
        }

        ///         /// We need this constructor to pass the Effort dbConnection to process data in memory
        ///         public SchoolContext(DbConnection dbConnection) : base(dbConnection, true)
        {
            
            Database.SetInitializer(
      new DropCreateDatabaseAlways());
        }

        public DbSet Student { get; set; }
                
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new StudentMap());

            base.OnModelCreating(modelBuilder);
        }
    }



Create StudentRepository and Student Service class to perform Insert/Update Operations.

IStudentRepository and StudentRepository



public interface IStudentRepository
    {
        Student GetStudent(int studentId);

        int AddStudent(Student student);

    }
 
 public class StudentRepository : IStudentRepository
    {
        private SchoolContext context;

        public StudentRepository(SchoolContext context)
        {
            this.context = context;
        }

        public Student GetStudent(int studentId)
        {
            return this.context.Student.Find(studentId);
        }

        public int AddStudent(Student student)
        {
            this.context.Student.Add(student);
            this.context.SaveChanges();

            return student.Id;
        }
    }



IStudentService and StudentService



 public interface IStudentService
    {
        Student GetStudent(int studentId);

        int AddStudent(Student student);
    }
 
   public class StudentService : IStudentService
    {
        private IStudentRepository _iStudentRepository;

        public StudentService(IStudentRepository iStudentRepository)
        {
            _iStudentRepository = iStudentRepository;
        }

        public Student GetStudent(int studentId)
        {
            return _iStudentRepository.GetStudent(studentId);
        }

        public int AddStudent(Student student)
        {
            return _iStudentRepository.AddStudent(student);
        }

    }



Unit Test Class

Add Effort.EF6 Nuget package to unit test library



 [TestClass]
    public class EntityFrameworkTest
    {
        private SchoolContext context;

        [TestInitialize]
        public void Initialize()
        {
            context = new SchoolContext(Effort.DbConnectionFactory.CreateTransient());
        }


        [TestMethod]
        public void TestConnection()
        {
            IStudentService studentService = new StudentService(new StudentRepository(context));

            studentService.AddStudent(new Student()
            {
                FirstName = "Murtaza",
                LastName = "Ali",
                Email = "murtaza@great",
                DOB = DateTime.Now,
                CollegeId = 75
            });

            var student = studentService.GetStudent(1);


        }
    }


No comments:

Post a Comment