Sunday, March 19, 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);


        }
    }


26 comments:

  1. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. Devops Training in Chennai

    Devops Training Institute in Chennai

    ReplyDelete
  2. Thanks. Glad it helped you in anyway.

    ReplyDelete

  3. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Best Web Designing Institute in Chennai
    Best web designing course in chennai
    web design classes
    PHP Training Center in Chennai
    PHP Institutes in Chennai
    PHP courses in chennai

    ReplyDelete
  4. Brilliant ideas that you have share with us for, RPA Training in Chennai

    ReplyDelete
  5. Its a wonderful post and very helpful, thanks for all this information.
    Vmware Training institute in Noida

    ReplyDelete
  6. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live.
    angularjs training in chennai | angularjs course in chennai | angularjs training institute in chennai | angularjs training institutes in chennai

    ReplyDelete
  7. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
    Datascience Course in Chennai | Datascience Training in Chennai

    ReplyDelete
  8. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.

    Best PHP Training Institute in Chennai|PHP Course in chennai
    Best .Net Training Institute in Chennai
    Matlab Training in Chennai
    Embedded Training in Chennai
    SAS Training in Chennai

    ReplyDelete
  9. The way of Explanation's about the content is ease to understand...Thanks for such a clear flow of content...Keep Doing this amazing works
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  10. I find this blog to be very interesting and very resourceful. I would say that your blogs are really interesting and informative for me and this article explained everything in detail.
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete