TransWikia.com

Unit Testing a Class With A Private Constructor

Stack Overflow Asked on January 16, 2021

I am trying to test a class that only has a private constructor. This is for a course registration system. The courses do not get create via our application, therefore we intentionally have no public constructor. Instead we use EF to get the courses that are already in the database, and register students to them.

I am trying to test the register method of the Course class, however I have no way of creating an instance. I could use
course = (Course)Activator.CreateInstance(typeof(Course), true);, but then I don’t have a way to setup the necessary properties since those are private.

What is the recommended approach for unit testing without a constructor?

This is a slimmed down version of the code.

public class Course
{
    private Course()
    {
    }

    public int Id { get; private set; }
    public string Name { get; private set; }
    public bool Open { get; private set; }
    public virtual ICollection<Student> Students { get; private set; }

    public void Register(string studentName)
    {
        if (Open)
        {
            var student = new Student(studentName);
            Students.Add(student);
        }
    }
}

// Usage //

using (var db = new SchoolContext())
        {
            var course = db.Courses.Include(x => x.Students).Where(x => x.Name == courseName).First();

            course.Register(studentName);
            db.SaveChanges();
        }

// Unit Test //

[Fact]
public void CanRegisterStudentForOpenClass(){
    // HERE I HAVE NO WAY TO CHANGE THE OPEN VARIABLE
    var course = (Course)Activator.CreateInstance(typeof(Course), true);
    course.Register("Bob");
}



    

5 Answers

Yes you can using reflexion. your code is neraly there;

you can get properties and fields of the types with typeof(Course).GetProperty("PropertyName") then you can use SetValue to set the desired value, and pass as parameter first the instance to modify then the value. in your case true;

note: in your example you will need to add the Collection of students too, if your Open is true.

Here there is a working example:

[Fact]
public void CanRegisterStudentForOpenClass()
{
    var course = (Course)Activator.CreateInstance(typeof(Course), true);

    typeof(Course).GetProperty("Open").SetValue(course, true, null);
    ICollection<Student> students = new List<Student>();
    typeof(Course).GetProperty("Students").SetValue(course, students, null);

    course.Register("Bob");

    Assert.Single(course.Students);
}

Correct answer by TiGreX on January 16, 2021

You can create a JSON representation of your default instance and deserialize it with Newtonsoft.

Something like this:

using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using privateConstructor;

namespace privateConstructorTest
{
    [TestClass]
    public class CourseTest
    {
        [TestMethod]
        public void Register_WhenOpenIsTrue_EnableAddStudents()
        {
            // Arrange
            const string json = @"{'Id': 1, 'name':'My Course', 'open':'true', 'students':[]}";
            var course = CreateInstance<Course>(json);

            // Act
            course.Register("Bob");

            // Assert
            Assert.AreEqual(1, course.Students.Count);
        }

        [TestMethod]
        public void Register_WhenOpenIsFalse_DisableAddStudents()
        {
            // Arrange
            const string json = @"{'Id': 1, 'name':'My Course', 'open':'false', 'students':[]}";
            var course = CreateInstance<Course>(json);

            // Act
            course.Register("Bob");

            // Assert
            Assert.AreEqual(0, course.Students.Count);
        }

        private static T CreateInstance<T>(string json) =>
            JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings
            {
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                ContractResolver = new ContractResolverWithPrivates()
            });

        public class ContractResolverWithPrivates : CamelCasePropertyNamesContractResolver
        {
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                var prop = base.CreateProperty(member, memberSerialization);
                if (prop.Writable) return prop;
                var property = member as PropertyInfo;
                if (property == null) return prop;
                var hasPrivateSetter = property.GetSetMethod(true) != null;
                prop.Writable = hasPrivateSetter;

                return prop;
            }
        }
    }
}

In order to have a cleaner test class, you can extract the JSON strings and the helper code that creates the instance.

Answered by Jairo Alfaro on January 16, 2021

As a few people have mentioned here, unit testing something private is either a code smell, or a sign you're writing the wrong tests.

In this case, what you would want to do is use EF's in-memory database if you're using Core, or mocking with EF6.

For EF6 You can follow the docs here

I would say rather than newing your dbContext where you do, pass it in via Dependency Injection. If that's beyond the scope of the work you're doing, (I'm assuming this is actual coursework, so going to DI may be overkill) then you can create a wrapper class that takes a dbcontext and use that in place.

Taking a few liberties with where this code is called from...


class Semester
{
  //...skipping members etc

  //if your original is like this
  public RegisterCourses(Student student)
  {
    using (var db = new SchoolContext())
    {
      RegisterCourses(student, db);
    }
  }

  //change it to this
  public RegisterCourses(Student student, SchoolContext db)
  {
    var course = db.Courses.Include(x => x.Students).Where(x => x.Name == courseName).First();

      course.Register(studentName);
      db.SaveChanges();
  }
}  

[Fact]
public void CanRegisterStudentForOpenClass()
{
  //following after https://docs.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking#testing-query-scenarios
  var mockCourseSet = new Mock<DbSet<Course>>();
  mockCourseSet.As<IQueryable<Course>>().Setup(m => m.Provider).Returns(data.Provider);
  mockCourseSet.As<IQueryable<Course>>().Setup(m => m.Expression).Returns(data.Expression);
  mockCourseSet.As<IQueryable<Course>>().Setup(m => m.ElementType).Returns(data.ElementType);
  mockCourseSet.As<IQueryable<Course>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
  //create an aditional mock for the Student dbset
  mockStudentSet.As.........

  var mockContext = new Mock<SchoolContext>();
  mockContext.Setup(c => c.Courses).Returns(mockCourseSet.Object);
  //same for student so we can include it

  mockContext.Include(It.IsAny<string>()).Returns(mockStudentSet); //you can change the isAny here to check for Bob or such

  
  var student = Institution.GetStudent("Bob");
  var semester = Institution.GetSemester(Semester.One);
  semester.RegisterCourses(student, mockContext);
}

If you're using EFCore you can follow it along from here

Answered by Cryolithic on January 16, 2021

You can fake private constructors and members using TypeMock Isolator or JustMock (both paid) or using MS Fakes (only available in VS Enterprise).

There is also a free Pose library that allows you to fake access to properties.

Unfortunately, the private constructor can't be forged. Therefore, you will need to create an instance of the class using reflection.


Add package.
Open namespace:

using Pose;

Test code:

[Fact]
public void CanRegisterStudentForOpenClass()
{
    var course = (Course)Activator.CreateInstance(typeof(Course), true);

    ICollection<Student> students = new List<Student>();
    Shim studentsPropShim = Shim.Replace(() => Is.A<Course>().Students)
                                .With((Course _) => students);

    Shim openPropShim = Shim.Replace(() => Is.A<Course>().Open)
                            .With((Course _) => true);

    int actual = 0;

    PoseContext.Isolate(() =>
    {
        course.Register("Bob");
        actual = course.Students.Count;
    },
    studentsPropShim, openPropShim);

    Assert.Equal(1, actual);
}

Answered by Alexander Petrov on January 16, 2021

If you would rather not use reflection, then I recommend you use internal classes (instead of private) and using the InternalsVisibleToAttribute on your implementation assembly.

You can find more about the attribute here. Here's a quick guide on how you can use it!

Step 1. Add this attribute to your assembly that wants its internal code tested.

[assembly: InternalsVisibleToAttribute("MyUnitTestedProject.UnitTests")]

Step 2. Change private to internal.

public class Course
{
    internal Course()
    {
    }

    public int Id { get; internal set; }
    public string Name { get; internal set; }
    public bool Open { get; internal set; }
    public virtual ICollection<Student> Students { get; internal set; }

    /* ... */
}

Step 3. Write your tests like normal!

[Fact]
public void CanRegisterStudentForOpenClass()
{
    var course = new Course();
    course.Id = "#####";
    course.Register("Bob");
}

Answered by Anthony Marmont on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP