无法实例化类错误的代理 [英] Cannot instantiate proxy of class error

查看:55
本文介绍了无法实例化类错误的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的单元测试,其中在创建课程时,标题"字段不能为空.我必须使用具有依赖注入与UnitOfWork的服务类对其进行测试.当我调试测试时,出现无法实例化类代理的异常错误:ContosoUniversity.Models.CourseRepository 我调查了该错误,但无法理解如何解决该问题,并且声明声明?

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?

错误消息显示图像

课程库

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));
}

推荐答案

该错误表明 CourseRepository 无法初始化,因为它没有参数少的构造函数.模拟框架首先寻找不带参数的构造函数来创建模拟对象.

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.

如果您的类没有无参数的构造函数,则在创建Mock时需要传递这些参数.在您的情况下, CourseRepository 的模拟将如下创建.

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);

代替null,您还可以传递构造函数参数的模拟对象.

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

这篇关于无法实例化类错误的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆