Reputation: 125
while interface mocking I am trying to send with actual inputs in model object, but getting failure with
Moq.MockException : ISearchRepo.GetEsDataWithBoolByAggregation<IndexVM>(BoolMustMatchAggregationIM) invocation failed with mock behavior Strict.
All invocations on the mock must have a corresponding setup.
error message.
Mock method for interface input object (which is sending same like in manual)
private BoolMustMatchAggregationIM GetBoolMustMatchAggregationIMMockData()
{
var objInputs = GetTopologyTabularApplyFilterMockData();
var studentFieldValue = objInputs.StudentId != null ? string.Join(',', objInputs.StudentId) : string.Empty;
BoolMustMatchAggregationIM topoplogyApplyFilter = new BoolMustMatchAggregationIM()
{
From = objInputs.From,
Size = objInputs.Size,
IndexName = ElasticSearchIndexConstant.Index,
FirstField = DataConstant.MarketField,
FirstFieldValue = objInputs.Market,
SecondField = DataConstant.StudentField,
SecondFieldValue = studentFieldValue,
ThirdField = DataConstant.SubjectField,
ThirdFieldValue = objInputs.SubjectId != null ? objInputs.SubjectId : null,
FourthField = DataConstant.SiteIdFieldName,
FourthFieldValue = objInputs.SiteId != null ? objInputs.SiteId[0] : null,
OperatorType = DataConstant.NestAndOperator,
CardinalField = DataConstant.CardinalField,
SumName = DataConstant.SumName,
MinimumMatch = DataConstant.One
};
return topoplogyApplyFilter;
}
Mock method for output object from interface
private IEnumerable<SiteSubjectIndexVM> GetSiteSubjectIndexClassMockData()
{
List<SiteSubjectDetails> objListSiteSubject = new List<SiteSubjectDetails>();
SiteSubjectDetails objSiteSubject = new SiteSubjectDetails { SubjectId = 4002453 };
objListSiteSubject.Add(objSiteSubject);
List<SiteSubjectIndexVM> objListSiteSubjectIndexClass = new List<SiteSubjectIndexVM>();
SiteSubjectIndexVM objSiteSubjectIndexClass = new SiteSubjectIndexVM()
{
Id = 123,
StudentId = "SE123",
Longitude = -122.51m,
Latitude = 47.66m,
Region = "Selected",
SiteId = "SE03D123",
HasSubjectSites = 1,
Market = "HNK",
TimeStamp = new DateTime(2022, 08, 07, 11, 02, 51, 167),
AAVStatus = "Selected"
};
objListSiteSubjectIndexClass.Add(objSiteSubjectIndexClass);
objSiteSubjectIndexClass = new SiteSubjectIndexVM()
{
Id = 456,
SiteId = "SE04D456",
Subjects = objListSiteSubject,
StudentId = "SE456",
Latitude = 47.74m,
Longitude = -122.15m,
Market = "WGL",
TimeStamp = new DateTime(2022, 08, 07, 11, 02, 51, 167),
Region = "WEST",
HasSubjectSites = 1
};
objListSiteSubjectIndexClass.Add(objSiteSubjectIndexClass);
return objListSiteSubjectIndexClass;
}
Test Case
[Fact]
public async Task GetTabularApplyFilterAsync_WhenMarketIsNotNull_ReturnsSiteSubjectsIndexFromES()
{
//Arrange
var objRepoIM = GetBoolMustMatchAggregationIMMockData();
var objRepoVM = GetSiteSubjectIndexClassMockData().Where(x=>x.Id== 72337696).ToList();
var objIMapperVM = GetSiteSubjectIndexMockData().Where(x => x.Id == 72337696).ToList();
var objServiceIM = GetTabularApplyFilterMockData();
objServiceIM.StudentId = null; objServiceIM.SubjectId = null; objServiceIM.SiteId = null;
//Action
_mockISearchRepo.Setup(x => x.GetEsDataWithBoolByAggregationAsync<SiteSubjectIndexVM>(objRepoIM)).ReturnsAsync(objRepoVM);
_mockMapper.Setup(x => x.Map<IEnumerable<SiteSubjectIndex>>(objRepoVM)).Returns(objIMapperVM);
var subjectService = this.CreateSearchService();
var result = await subjectService.GetTabularApplyFilterAsync(objServiceIM).ConfigureAwait(false);
//Assert
Assert.NotNull(result);
Assert.Equal(result, objIMapperVM);
_mockRepository.VerifyAll();
}
by _mockIElasticSearchRepo.Setup(x => x.GetEsDataWithBoolByAggregationAsync<SiteLinkIndexVM>(It.IsAny<BoolMustMatchAggregationIM>())).ReturnsAsync(objRepoVM);
error get resolved, but why? where I have given correct input model with correct values.
Upvotes: 0
Views: 223
Reputation: 22456
The error message signals that there is no setup for the call. The following setup will be met, if the parameter is equal to the object that you created:
_mockISearchRepo.Setup(x => x.GetEsDataWithBoolByAggregationAsync<SiteSubjectIndexVM>(objRepoIM)).ReturnsAsync(objRepoVM);
Most likely, the call to GetEsDataWithBoolByAggregationAsync
in the code under test does not use the exact same instance that you created in your test, but creates an instance of its own. Hence check will fail because it is not the same object and the setup is not matched.
You can work around this by defining the setup so that you check the parameters dynamically, e.g.
_mockISearchRepo
.Setup(x => x.GetEsDataWithBoolByAggregationAsync<SiteSubjectIndexVM>(It.Is<BoolMustMatchAggregationIM>(y => y.IndexName == ElasticSearchIndexConstant.Index))
.ReturnsAsync(objRepoVM);
In above sample, I have only included a very simple check for the index name. You can extend the expression to check the input more thoroughly.
By using this approach, the setup will be matched as soon as the expression returns true so this also works if the code under test creates the instance that is used as the parameter.
Upvotes: 1