C# NSubstitute mock不带参数的void方法
我是NSubstitute的新手,我试图用2个C# NSubstitute mock不带参数的void方法,c#,unit-testing,testing,mocking,nsubstitute,C#,Unit Testing,Testing,Mocking,Nsubstitute,我是NSubstitute的新手,我试图用2个out参数来模拟void方法,我很确定我做得不对 我有一个CustomerDataAccess类,它有一个具有以下签名的方法: void GetCustomerWithAddresses(int customerId, out List<Customer> customers, out List<Address
out
参数来模拟void
方法,我很确定我做得不对
我有一个CustomerDataAccess
类,它有一个具有以下签名的方法:
void GetCustomerWithAddresses(int customerId,
out List<Customer> customers,
out List<Address> addresses);
out
参数使用其参数位置作为索引进行更新。这在返回的中有解释。因此,对于您的特定情况,您正在填充第二个和第三个参数,因此您应该如下设置您的呼叫:
customerDataAccess.When(x => x.GetCustomerWithAddresses(1, out customers, out addresses))
.Do(x =>
{
x[1] = new List<Customer>() { new Customer() { CustomerId = 1, CustomerName = "John Doe" } };
x[2] = new List<Address>() { new Address() { AddressId = 1, AddressLine1 = "123 Main St", City = "Atlanta" } };
});
customerDataAccess.When(x=>x.GetCustomerWithAddresses(1,out客户,out地址))
.Do(x=>
{
x[1]=new List(){new Customer(){CustomerId=1,CustomerName=“John Doe”};
x[2]=new List(){new Address(){AddressId=1,AddressLine1=“123 Main St”,City=“Atlanta”};
});
对于非void方法,可以使用常规返回语法:
var haveWithAddresses = customerDataAccess.GetCustomerWithAddresses(1, out customers, out addresses)
.Returns(callInfo => {
callInfo[0] = new List<Customer>();
callInfo[1] = new List<Address>();
return true;
});
var haveWithAddresses=customerDataAccess.GetCustomerWithAddresses(1,out客户,out地址)
.Returns(callInfo=>{
callInfo[0]=新列表();
callInfo[1]=新列表();
返回true;
});
使用Void
方法时,When…Do
语法正确。是的,我没有访问数组中的特定参数。
var haveWithAddresses = customerDataAccess.GetCustomerWithAddresses(1, out customers, out addresses)
.Returns(callInfo => {
callInfo[0] = new List<Customer>();
callInfo[1] = new List<Address>();
return true;
});