Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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#Moq模拟ElasticSearch客户端_C#_Unit Testing_Testing_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Moq - Fatal编程技术网 elasticsearch,moq,C#,Unit Testing,Testing,elasticsearch,Moq" /> elasticsearch,moq,C#,Unit Testing,Testing,elasticsearch,Moq" />

使用C#Moq模拟ElasticSearch客户端

使用C#Moq模拟ElasticSearch客户端,c#,unit-testing,testing,elasticsearch,moq,C#,Unit Testing,Testing,elasticsearch,Moq,我正在测试我的类ElasticUtility,它需要ElasticClient的一个实例才能正常工作,所以我模拟了这样的类并将其注入ElasticUtility实例(utility) 编辑:GetPvData()方法: public IEnumerable GetPvData(字符串组ID、日期时间开始时间、日期时间结束时间) { var res=ElasticClient.Search(s=>s .索引(ElasticIndexName) .Filter(f=> f、 术语(t=>t.Hist

我正在测试我的类
ElasticUtility
,它需要
ElasticClient
的一个实例才能正常工作,所以我模拟了这样的类并将其注入
ElasticUtility
实例(
utility

编辑:
GetPvData
()方法:

public IEnumerable GetPvData(字符串组ID、日期时间开始时间、日期时间结束时间)
{
var res=ElasticClient.Search(s=>s
.索引(ElasticIndexName)
.Filter(f=>
f、 术语(t=>t.HistoryId,groupId)&&
f、 范围(i=>
i、 OnField(a=>a.DateTime).LowerOrEquals(startTime)).SortAscending(p=>p.DateTime).Size(1)).Documents.ToList();
return res.ToArray();
}
已解决:

   var segResp = new SearchResponse<SegmentRecord>();
    elasticClientMock.Setup(ec => ec.Search(
        It.IsAny<Func<SearchDescriptor<SegmentRecord>,
                SearchDescriptor<SegmentRecord>>>()))
        .Returns(segResp);
var segResp=newsearchresponse();
elasticClientMock.Setup(ec=>ec.Search(
It.IsAny())
.返回(SEGREP);

发生
NullReferenceException
异常是因为您没有在
search
方法上指定行为。您的
search
方法返回null,然后在null上调用
.Document

指定行为的方法如下所示:

elasticClientMock.Setup(x => x.Search<SegmentRecord>(
                             It.IsAny</* put here the right Func */>))
        .Returns( /* put here the instance you want to return */);
elasticClientMock.Setup(x=>x.Search(
是的,我是
.Returns(/*将要返回的实例放在此处*/);

你必须用正确的类型替换我的评论。

请添加
uhelasticity
的任务和
GetPvData
的实现,这可能是因为它受到了保护吗?没有受保护的任务不是问题。。。但是我询问了
uhelasticity
C'tor和
GetPvData
…这是GetPvData()的实现问题是
。Documents
您没有设置期望值,然后搜索return
null
…我无法创建Nest.ISearchResponse的实例来将其传递给.Returns()如果我写入:var test=Nest.ISearchResponse();它表示“此时接口名称无效”。哦,这是一个界面!即使使用Nest.SearchResponse也不起作用使用
Moq
创建
Nest.ISearchResponse()的instance或创建自定义类型,如果使用
Moq
请确保没有忘记初始化伪对象属性。是的,但它不起作用。我将响应模拟为:var response=newmock();elasticClientMock.Setup(ec=>ec.Search(It.IsAny())).Returns(response.Object);但是.Search()函数始终返回null
    protected ElasticUtility(IElasticClient elasticClient, string elasticIndexName)
    {
        this.ElasticClient = elasticClient;
        this.ElasticIndexName = elasticIndexName;
    }
    public IEnumerable<dynamic> GetPvData(string groupId, DateTime startTime, DateTime endTime)
    {
        var res = ElasticClient.Search<SegmentRecord>(s => s
            .Index(ElasticIndexName)
            .Filter(f =>
                f.Term(t => t.HistoryId, groupId) &&
                f.Range(i =>
                    i.OnField(a => a.DateTime).LowerOrEquals(startTime))).SortAscending(p => p.DateTime).Size(1)).Documents.ToList();

        return res.ToArray();
    }
   var segResp = new SearchResponse<SegmentRecord>();
    elasticClientMock.Setup(ec => ec.Search(
        It.IsAny<Func<SearchDescriptor<SegmentRecord>,
                SearchDescriptor<SegmentRecord>>>()))
        .Returns(segResp);
elasticClientMock.Setup(x => x.Search<SegmentRecord>(
                             It.IsAny</* put here the right Func */>))
        .Returns( /* put here the instance you want to return */);