2015-05-19 57 views
6

我是新來的NSubstitute,我試圖嘲笑void方法與2 out參數,我很確定我做錯了。NSubstitute模擬一個無效的方法與參數

我有一個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); 

回答

11

out參數是使用其參數位置作爲索引進行更新。這在Returnsdocumentation 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" } }; 
}); 
+0

是的工作我缺少訪問數組中的特定參數。 – NathanFisherSdk