【问题标题】:How to moq function with out parameter with any value如何使用任何值的 out 参数最小化函数
【发布时间】:2023-01-11 17:59:29
【问题描述】:

我的界面和类:

public interface IService
{
    public bool Test(string[] Ids, out string Id);
}

public class Service: IService
{
    public bool Test(string[] Ids, out string Id)
    {
        Id = Guid.NewGuid().ToString();
        return Ids.Contains(Id);
    }
}

不允许更改上面的任何代码,我必须 moq 这个函数才能在 UT 中抛出异常。

所以我们有var MoqService = new Mock<IService>(); 尝试了以下代码

MoqService.Setup(s => s.Test(It.IsAny<string[]>(), out It.Ref<string>.Any)
string id = It.IsAny<string>();
MoqService.Setup(s => s.Test(It.IsAny<string[]>(), out id)

它们都不起作用。

【问题讨论】:

    标签: c# visual-studio unit-testing moq


    【解决方案1】:

    也许试试

    string id = string.Empty;
    MoqService.Setup(s => s.Test(It.IsAny<string[]>(), out id)
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2020-05-16
      • 2017-08-18
      • 2021-06-08
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多