我是NSubstitute的新手,我试图使用2个void
参数模拟out
方法,我很确定我做错了。
我有一个CustomerDataAccess
类,其方法具有以下签名:
void GetCustomerWithAddresses(int customerId,
out List<Customer> customers,
out List<Address> addresses);
CustomerRepository
调用其GetCustomer
方法,然后调用CustomerDataAccess.GetCustomerWithAddresses
DAL方法。然后,DAL方法输出两个out
参数,一个用于客户,一个用于地址。然后,存储库方法使用AutoMapper
将DAL方法中的两个对象映射到存储库随后返回的业务域。
这是我到目前为止的代码,它不起作用。我的研究并没有帮助我确定解决这个问题需要做些什么。如何设置out
参数的值?
// Arange
ICustomerDataAccess customerDataAccess = Substitute.For<ICustomerDataAccess>();
IList<Customer> customers;
IList<Address> addresses;
customerDataAccess.When(x => x.GetCustomerWithAddresses(
1, out customers, out addresses))
.Do(x =>
{
customers = new List<Customer>() { new Customer() { CustomerId = 1, CustomerName = "John Doe" } };
addresses = new List<Address>() { new Address() { AddressId = 1, AddressLine1 = "123 Main St", City = "Atlanta" } };
});
CustomerRepository sut = new CustomerRepository(customerDataAccess);
// Act
Customer customer = sut.GetCustomer(1);
// Assert
Assert.IsNotNull(customer);
答案 0 :(得分:16)
out
参数使用其参数位置作为索引进行更新。它在Returns
documentation for NSubstitute中进行了解释。因此,对于您的特定情况,您正在填充第二个和第三个参数,因此您应该像这样设置您的呼叫:
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" } };
});
答案 1 :(得分:0)
对于非void方法,可以使用常规返回语法:
var haveWithAddresses = customerDataAccess.GetCustomerWithAddresses(1, out customers, out addresses)
.Returns(callInfo => {
callInfo[0] = new List<Customer>();
callInfo[1] = new List<Address>();
return true;
});
使用Void
方法的When...Do
语法是正确的。