Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot instantiate proxy of class error

I am doing a simple unit test where when creating a Course, the Title field cannot be empty. I am having to test it with a service class that has Dependency Injection with UnitOfWork. When I debug my test, I am getting an Exception error of Can not instantiate proxy of class: ContosoUniversity.Models.CourseRepository I looked into the error, but am not able to understand how to fix the issue and the Assert statement?

Error Message Display Image

CourseRepository

public class CourseRepository : GenericRepository<Course>
{
    public CourseRepository(SchoolContext context)
        : base(context)
    {
    }

UnitOfWork

public class UnitOfWork : IDisposable, IUnitOfWork
{
    private SchoolContext context = new SchoolContext();
    private GenericRepository<Department> departmentRepository;
    private CourseRepository courseRepository;

    public CourseRepository CourseRepository
    {
        get
        {
            if (this.courseRepository == null)
            {
                this.courseRepository = new CourseRepository(context);
            }
            return courseRepository;
        }
    }

    public virtual CourseRepository GetCourseRepository()
    {
        if (this.courseRepository == null)
        {
            this.courseRepository = new CourseRepository(context);
        }
        return courseRepository;
    }

CourseService

public class CourseService : ICourseService
{
    private IUnitOfWork unitOfWork;

    public CourseService (IUnitOfWork unitOfWork)
    {
        this.unitOfWork = unitOfWork;
    }

    public void Create(Course course)
    {
        unitOfWork.GetCourseRepository().Insert(course);
        unitOfWork.Save();
    }

    public Course GetCourseByID(int id)
    {
        return unitOfWork.GetCourseRepository().GetByID(id);
    }

TestMethod

[TestMethod]
public void TestMethod1()
{
    var course = new Course
    {
        CourseID = 2210,
        Title = string.Empty,
        Credits = 3,
        DepartmentID = 1
    };

    Mock<CourseRepository> mockRepo = new Mock<CourseRepository>();
    mockRepo.Setup(m => m.GetByID(course.CourseID)).Returns(course);

    var mockUnit = new Mock<IUnitOfWork>();
    mockUnit.Setup(x => x.GetCourseRepository()).Returns(mockRepo.Object);

    var myService = new CourseService(mockUnit.Object);
    myService.Create(course);

    //var error = _modelState["Title"].Errors[0];
    //Assert.AreEqual("The Title field is required.", error.ErrorMessage);

    //mockRepo.Setup(x => x.Insert(course));
}
like image 946
Afton Avatar asked Feb 15 '17 16:02

Afton


2 Answers

The error says that the CourseRepository can not be initialized because it does not have parameter less constructor. Mocking framework looks for parameter less constructor first to create mock object.

If your class does not have parameterless constructor then you need to pass those parameters when you create Mock. In your case mock of CourseRepository would be created as following.

var repositoryMock = new Mock<CourseRepository>(null); 

Instead of null, you can pass mock objects of the constructor parameters also.

like image 132
Chetan Avatar answered Sep 17 '22 08:09

Chetan


Just use the interface when mocking.

Mock<ICourseRepository> mockRepo = new Mock<ICourseRepository>();
mockRepo.Setup(m => m.GetByID(course.CourseID)).Returns(course);
like image 44
Ε Г И І И О Avatar answered Sep 20 '22 08:09

Ε Г И І И О