[英]How to pass class object type of Lazy to constructor while mocking using MOQ
public class StudentTests
{
private readonly Mock<IStudentRepository> studentRepository;
private readonly Mock<Lazy<IDepartmentService>> departmentService;
private readonly Mock<IStudentService> studentService;
private readonly Student student;
public StudentTests()
{
this.studentRepository = new Mock<IStudentRepository>();
this.departmentService = new Mock<Lazy<IDepartmentService>>();
this.studentService = new Mock<IStudentService>();
this.student = new Student(departmentService.Object, studentRepository.Object, studentService.Object);
}
}
The above code in which IDepartmentService is of Lazy type and while passing it to constructor it is giving error. 上面的代码,其中IDepartmentService是Lazy类型,并且在将它传递给构造函数时它给出了错误。 As Lazy class type object accessed through ".Value" and in MOQ use ".Object"
通过“.Value”和MOQ使用“.Object”访问的Lazy类类型对象
Thanks in advance for the help! 先谢谢您的帮助! .
。
You do not need to mock Lazy
object, instead, you mock the object Lazy
returns and then create Lazy
manually like this: 你不需要模拟
Lazy
对象,而是模拟对象Lazy
返回,然后像这样手动创建Lazy
:
new Lazy<IDepartmentService>(() => departmentService.Object)
So you get: 所以你得到:
private readonly Mock<IDepartmentService> departmentService;
...
this.student = new Student(new Lazy<IDepartmentService>(() => departmentService.Object), studentRepository.Object, studentService.Object);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.