【发布时间】:2021-08-16 05:19:52
【问题描述】:
我需要模拟简单的类:
namespace Infrastructure.Repositores
{
public class StudentRepository : IStudentRepository
{
private readonly GradeBookDBContext _dbContext;
public StudentRepository(GradeBookDBContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<Student> GetAllStudents()
{
var students = _dbContext.Students;
return students;
}
我尝试做这样的事情,但它不起作用......我是模拟测试的初学者,谁能解释我为什么它不起作用?
[Fact]
public void Test2()
{
Mock<IStudentRepository> studentRepositoryMock =
new Mock<IStudentRepository>();
studentRepositoryMock.Setup(x => x.GetAllStudents())
.Returns(SampleStudents);
StudentRepository ss = new StudentRepository();
var expected = SampleStudents();
var actual = ss.GetAllStudents();
Assert.Equal(expected, actual);
}
private List<Student> SampleStudents()
{// list of sample objects}
如何模拟这个类保持良好的做法?
【问题讨论】:
-
你可以使用起订量库github.com/Moq/moq4
-
@AlexanderMokin 好的,但我的解决方案不起作用
-
这篇文章在哪里是模拟的?