设置 returns 字符串数组的集成测试方法

Setup integration test method that returns a string array

我有从数据库中读取和 returns 数据的方法,现在这个方法检查 returned 模型是否不为 null,如果不为空,它会获取我想要的字段return 和 return 将它们放入一个数组中,如果是字符串,这里是方法

public string[] GetCustomerSecret(string publicKey)
{
    var customer = _directConnectContext.Customers.Single(c => c.PublicKey == publicKey);
    if (null != customer)
    {
        return new[] { customer.PrivateKey, customer.HttpReferer ?? string.Empty};
    }        
    return null;
}

所以我想设置集成或单元测试(不确定它到底是哪一种)方法来测试,我正在使用 Mock,到目前为止我的测试一直运行良好,但我正在为这个特定的测试而苦苦挣扎,这是我尝试过的,下面的代码模拟了客户列表

List<Customer> customerList = new List<Customer>()
{
    new Customer { 
        CustomerID = 1,
        PublicKey = "11111",
        PrivateKey = "121112A",
        PartnerName = "TestParter",
        PartnerIata = "56858VB",
        PartnerID = "67607", 
        PartnerActive = true,
        HttpReferer="RefererTest"
    }
};

下面是我需要帮助的实际设置

customerMock
    .Setup(x => x.GetCustomerSecret(It.IsAny<string>()))
    .Returns((string publicKey) => customerList.Where(x => x.PublicKey == publicKey)).ToArray());

它给出了一个错误,内容为

"cannot convert lambda expression to type because it is not a delegate"

假设目标是复制实际实施中所做的事情,然后使用模拟设置执行类似的操作。

customerMock
    .Setup(_ => _.GetCustomerSecret(It.IsAny<string>()))
    .Returns((string publicKey) => {
        var customer = customerList.Single(x => x.PublicKey == publicKey);
        return new[] { customer.PrivateKey, customer.HttpReferer ?? string.Empty};
    });