【问题标题】:Setup integration test method that returns a string array设置返回字符串数组的集成测试方法
【发布时间】:2018-03-16 13:35:13
【问题描述】:

我有从数据库读取和返回数据的方法,现在这个方法检查返回的模型是否不为空,如果不是,它会获取我想要返回的字段并在数组中返回它们,如果是字符串,这里是方法

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());

它给出的错误是,

"无法将 lambda 表达式转换为类型,因为它不是 委托”

【问题讨论】:

    标签: c# unit-testing lambda integration-testing moq


    【解决方案1】:

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

    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};
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      相关资源
      最近更新 更多