Cannot seem to moq EF CodeFirst 4.1.Help anyone?

后端 未结 2 1043
礼貌的吻别
礼貌的吻别 2021-01-01 07:25

I have been given the task to evaluate codeFirst and possible to use for all our future projects. The evaluation is based on using codeFirst with an existing database.

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 07:53

    The issue from what I can see is that you are throwing away the mock object and newing up a new instance

    _studentRepository = new StudentRepository(ctx);
    

    Perhaps add a method on the interface to add the context object and reuse the same instance that was injected in the constructor.

    using (var ctx = new SchoolContext("SchoolDB"))
        {
            _studentRepository.Context = ctx;
            var students = _studentRepository.GetAll().ToList();
            return students;
        } 
    }
    

提交回复
热议问题