Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在谓词参数上使用NSubstitute返回?_C#_Predicate_Nsubstitute - Fatal编程技术网

C# 如何在谓词参数上使用NSubstitute返回?

C# 如何在谓词参数上使用NSubstitute返回?,c#,predicate,nsubstitute,C#,Predicate,Nsubstitute,NSubstitute新手,在模拟采用谓词的方法调用的返回时遇到困难。 例如,我在主代码中有这个 var currReport = this.ClientRepo.Get<Report>(x => x.Uid == reportUid).FirstOrDefault(); var currReport=this.ClientRepo.Get(x=>x.Uid==reportUid.FirstOrDefault(); 我想在我的测试中做类似的事情 var parentRepo

NSubstitute新手,在模拟采用谓词的方法调用的返回时遇到困难。 例如,我在主代码中有这个

var currReport = this.ClientRepo.Get<Report>(x => x.Uid == reportUid).FirstOrDefault();
var currReport=this.ClientRepo.Get(x=>x.Uid==reportUid.FirstOrDefault();
我想在我的测试中做类似的事情

var parentReport = new Report(){Uid = request.ParentReportUid, Name = "Test"};
this.clientRepository.Get(Arg.Is<Expression<Func<Report, bool>>>(expr => Lambda.Eq(expr, i => i.Uid == request.ParentReportUid))).Returns(new List<Report>() { parentReport }.ToArray());
var parentReport=new Report(){Uid=request.ParentReportUid,Name=“Test”};
this.clientRepository.Get(Arg.Is(expr=>Lambda.Eq(expr,i=>i.Uid==request.ParentReportUid)).Returns(new List(){parentReport}.ToArray());
这是行不通的。我已确认request.ParentReportUid与实际方法调用中的reportUid匹配。但它仍然返回空值。如果我切换到Arg.Any,那么它将返回报告,如下所示

 this.clientRepository.Get(Arg.Any<Expression<Func<Report, bool>>>()).Returns(new List<Report>() { parentReport }.ToArray());
this.clientRepository.Get(Arg.Any()).Returns(newlist(){parentReport}.ToArray());
这是我试图模拟的实际方法的签名

 T[] Get<T>(System.Linq.Expressions.Expression<Func<T, bool>> predicate = null);
T[]Get(System.Linq.Expressions.Expression谓词=null);
请告知。
谢谢

尝试使用NSubstitute类Arg
公共静态T Is(表达式谓词)
方法。 您没有在谓词中指定什么是类型X

我已经投入了一些时间,并且已经有了解决方案。这是一个Neleus.LambdaCompare Nuget包。您可以使用Lambda.Eq方法。我试过了,效果很好。 在您的示例中,它应该类似于:

this.Repo.Get<Report>(Arg.Is<Expression<Func<Report, bool>>>(expr => Lambda.Eq(expr, i => i.ParentType == "1AType" && i.OwnerUid == 5))).Returns(reports);
this.Repo.Get(Arg.Is(expr=>Lambda.Eq(expr,i=>i.ParentType==“1AType”&&i.OwnerUid==5)))。返回(报告);
这是我尝试过的一个示例,测试结果为绿色。这个示例与您的签名匹配

public class ExpresionClass : IExpresionClass
{
    T[] IExpresionClass.Get<T>(Expression<Func<T, bool>> predicate)
    {
        throw new NotImplementedException();
    }
}

public interface IExpresionClass
{
    T[] Get<T>(Expression<Func<T, bool>> predicate = null);
}

public interface ITestClass
{
    Person[] GetPerson();
}

public class Person
    {
        public string ParentType { get; set; }

        public int OwnerUid { get; set; }
    }

public class TestClass : ITestClass
{
    private readonly IExpresionClass expressionClass;

    public TestClass(IExpresionClass expressionClass)
    {
        this.expressionClass = expressionClass;
    }

    public Person[] GetPerson()
    {
        var test = expressionClass.Get<Person>(x => x.ParentType == "1AType" && x.OwnerUid == 10);

        return test;
    }

}

[TestMethod]
    public void DoesLinqMatch()
    {
        var person = new Person();
        person.OwnerUid = 59;
        person.ParentType = "ParentType";

        Person[] personsarray = new Person[] { person };
        var expressionClass = Substitute.For<IExpresionClass>();
        expressionClass.Get(Arg.Is<Expression<Func<Person, bool>>>(expr => Lambda.Eq(expr, i => i.ParentType == "1AType" && i.OwnerUid == 10))).Returns(personsarray);

        var cut = new TestClass(expressionClass);
        var persons = cut.GetPerson();

        persons.First().ParentType.Should().Be("ParentType");
    }
公共类ExpresionClass:IExpresionClass
{
T[]IExpresionClass.Get(表达式谓词)
{
抛出新的NotImplementedException();
}
}
公共接口IExpresionClass
{
T[]Get(表达式谓词=null);
}
公共接口ITestClass
{
Person[]GetPerson();
}
公共阶层人士
{
公共字符串父类型{get;set;}
public int OwnerUid{get;set;}
}
公共类TestClass:ITestClass
{
私有只读IExpresionClass expressionClass;
公共测试类(IExpresionClass expressionClass)
{
this.expressionClass=expressionClass;
}
公众人物[]GetPerson()
{
var test=expressionClass.Get(x=>x.ParentType==“1AType”&&x.OwnerUid==10);
回归试验;
}
}
[测试方法]
public void DoesLinqMatch()
{
var person=新的person();
person.OwnerUid=59;
person.ParentType=“ParentType”;
Person[]personsarray=新的Person[]{Person};
var expressionClass=Substitute.For();
expressionClass.Get(Arg.Is(expr=>Lambda.Eq(expr,i=>i.ParentType==“1AType”&&i.OwnerUid==10)).Returns(personsarray);
var cut=新的测试类(expressionClass);
var persons=cut.GetPerson();
persons.First().ParentType.Should().Be(“ParentType”);
}

我认为(请原谅自我推销:)可以满足您的需要。如果没有,请让我知道,我会尽力提供更多信息。或者,如果您不需要检查传入的实际谓词,这是非常好的。您是对的。在这种情况下,它调用了错误的方法重载。“我会尽力调查的。”戈茨我认为有解决办法。我已经编辑了我的答案。请您尝试一下,现在让我看看是否有效。看起来很有希望,但不幸的是,当调用代码调用传入谓词的方法时,我仍然得到null:(感谢您的尝试!我在问题的底部添加了签名感谢您的努力。我再次尝试,但没有成功。如果我遗漏了一些明显的内容,我已更新了上面的问题,以显示我正在使用的实际方法调用和测试返回。也许您可以看到一些不同的内容。不要被不同的C名称所抛弃他们是一样的。