Moq:如何根据方法参数返回结果?
ICustomerRepository 定义:Customer GetCustomerByID(int CustomerID);
//Arrange
var customers = new Customer()
{
new Customer() { CustomerID = 1, Name = "Richard" },
new Customer() { CustomerID = 2, Name = "Evan" },
new Customer() { CustomerID = 3, Name = "Marie-France" },
}.AsQueryable();
Mock<ICustomerRepository> mock = new Mock<ICustomerRepository>();
我如何告诉 Moq 根据 CustomerID 参数返回正确的客户???
我已经能够设置第一部分,但不能设置返回对象。
mock.Setup(m => m.GetCustomerByID(It.Is<int>(i => i >= 0))).Returns(/// To be define///)
想法是得到与此相同的结果:
public Customer GetCustomerByID(int CustomerID)
{
return customers.FirstOrDefault(c => c.CustomerID == CustomerID);
}
感谢您的帮助
ICustomerRepository define: Customer GetCustomerByID(int CustomerID);
//Arrange
var customers = new Customer()
{
new Customer() { CustomerID = 1, Name = "Richard" },
new Customer() { CustomerID = 2, Name = "Evan" },
new Customer() { CustomerID = 3, Name = "Marie-France" },
}.AsQueryable();
Mock<ICustomerRepository> mock = new Mock<ICustomerRepository>();
How do I tell Moq to return the correct customer depending on the CustomerID paremeter???
I've been able to set up the first part, but not the return object.
mock.Setup(m => m.GetCustomerByID(It.Is<int>(i => i >= 0))).Returns(/// To be define///)
The Idea is to have the same result as this:
public Customer GetCustomerByID(int CustomerID)
{
return customers.FirstOrDefault(c => c.CustomerID == CustomerID);
}
Thanks for helping
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将客户设置为
List
- 无需 AsQueryable。Just make customers a
List<Customer>
- no need for AsQueryable.