了解 Moq 的 Setup() 函数
Understanding Moq's Setup() function
我对 Setup()
有点困惑。
根据我在声明时的理解:
Mock<IUnitOfWork> uwork = new Mock<IUnitOfWork>();
我们正在创建一个模拟存储库,该存储库永远实际上 无法访问数据库。因为它从不接触数据库,所以我们必须给它一些模拟数据。
例如:
Question question = new Question {
Title = "General question",
Message = "Message body text..."
}
这是我有点困惑的地方。根据我的理解,我们告诉我们的 Mocked 存储库 what 数据到 return 并且在 which 情况下到 return 它。
// in this circumstance // return this
uwork.Setup(i =. i.QuestionsRepository.GetById(1)).Returns(question)
此时我们创建一个控制器实例并将uwork.object传递给控制器实例。当控制器调用 (circumstance) 方法时,我们的 Mock 存储库会生成我们指定的 return 值。
问题
这是正确的吗? 如果不是 在这里停下来纠正我。 如果是这样,那么为什么这样的东西不起作用,我该如何解决这个问题?
控制器:
uwork.QuestionRepository.GetAll().Where(l => l.Message_Id == id).ToList();
测试控制器:
uwork.Setup(i => i.QuestionsRepository
.GetAll().Where(l => l.Message_Id == 1).ToList())
.Returns(questions);
// questions is a List<Question>
我遇到异常:
An exception of type 'System.NotSupportedException' occurred in
Moq.dll but was not handled in user code
Additional information: Expression references a method that does not
belong to the mocked object: i =>
i.LegalInquiryRepository.GetAll().Where(l =>
l.legalCommunication_Id ==
设置中包含的 Where() 和 ToList() 导致了错误。你没有试过吗?
uwork.Setup(i => i.QuestionsRepository.GetAll()).Returns(questions);
你到底想做什么?
您收到该异常是因为您正在尝试设置一个方法 (Where
) 不 属于模拟 (uwork
).
您需要先设置 i.QuestionRepository
属性,然后设置 GetAll
方法。
Where
方法(假设它是为 IQueryable
定义的方法)不能被模拟,因为它是静态的 - 但没关系。只需确保源集合具有正确的元素,Where
将 select 它们。
var questionsRepoMock = //...
uwork.SetupGet(i => i.QuestionsRepository).Returns(questionsRepoMock.Object);
questionsRepoMock.Setup(r => r.GetAll())
.Returns(questions);
我对 Setup()
有点困惑。
根据我在声明时的理解:
Mock<IUnitOfWork> uwork = new Mock<IUnitOfWork>();
我们正在创建一个模拟存储库,该存储库永远实际上 无法访问数据库。因为它从不接触数据库,所以我们必须给它一些模拟数据。
例如:
Question question = new Question {
Title = "General question",
Message = "Message body text..."
}
这是我有点困惑的地方。根据我的理解,我们告诉我们的 Mocked 存储库 what 数据到 return 并且在 which 情况下到 return 它。
// in this circumstance // return this
uwork.Setup(i =. i.QuestionsRepository.GetById(1)).Returns(question)
此时我们创建一个控制器实例并将uwork.object传递给控制器实例。当控制器调用 (circumstance) 方法时,我们的 Mock 存储库会生成我们指定的 return 值。
问题
这是正确的吗? 如果不是 在这里停下来纠正我。 如果是这样,那么为什么这样的东西不起作用,我该如何解决这个问题?
控制器:
uwork.QuestionRepository.GetAll().Where(l => l.Message_Id == id).ToList();
测试控制器:
uwork.Setup(i => i.QuestionsRepository
.GetAll().Where(l => l.Message_Id == 1).ToList())
.Returns(questions);
// questions is a List<Question>
我遇到异常:
An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code
Additional information: Expression references a method that does not belong to the mocked object: i => i.LegalInquiryRepository.GetAll().Where(l => l.legalCommunication_Id ==
设置中包含的 Where() 和 ToList() 导致了错误。你没有试过吗?
uwork.Setup(i => i.QuestionsRepository.GetAll()).Returns(questions);
你到底想做什么?
您收到该异常是因为您正在尝试设置一个方法 (Where
) 不 属于模拟 (uwork
).
您需要先设置 i.QuestionRepository
属性,然后设置 GetAll
方法。
Where
方法(假设它是为 IQueryable
定义的方法)不能被模拟,因为它是静态的 - 但没关系。只需确保源集合具有正确的元素,Where
将 select 它们。
var questionsRepoMock = //...
uwork.SetupGet(i => i.QuestionsRepository).Returns(questionsRepoMock.Object);
questionsRepoMock.Setup(r => r.GetAll())
.Returns(questions);