Mock method with Expression as parameter. #2644
-
Hi, I need to mock the this.catalogueRepository.GetAsync(predicate).ConfigureAwait(false); in the below method public async Task<Catalogue> AddProductDetails(int a)
{
Expression<Func<Catalogue, bool>> predicate = a => a.ParentId == viewModel.VendorId &&
a.GlobalKey == EYConstants.GlobalKeys.Product &&
!a.IsDeleted &&
a.GlobalValue.ToLower() == viewModel.ProductName.ToLower().Trim();
var data = await this.catalogueRepository.GetAsync(predicate).ConfigureAwait(false);
return data;
} I tried to mock the this.catalogueRepository.GetAsync method in the below approach. But it returns null all the time. Xunit test. [Fact]
public async Task AddProductDetails_ReturnResult_WhenInputIsValid()
{
#region Arrange
var viewModel = GetAddProductRequest();
var vendorDetail = GetVendorDetail();
var response = GetAddProductResponse();
Expression<Func<Catalogue, bool>> predicate = a => a.ParentId == viewModel.VendorId &&
a.GlobalKey == EYConstants.GlobalKeys.Product &&
!a.IsDeleted &&
a.GlobalValue.ToLower() == viewModel.ProductName.ToLower().Trim();
#endregion
#region Act
//Mocking the method with expression as parameter.
this.MockCatalogueRepository.Setup(x => x.GetAsync(predicate).ReturnsAsync(vendorDetail);
var result = await manager.AddProductDetails(viewModel);
#endregion
#region Assert
Assert.NotNull(result);
Assert.IsType<string>(result);
Assert.Equal(response, result);
#endregion
} Kindly help me to resolve the issue. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This isn't strictly speaking a question for xUnit.net, since we don't provide a mocking framework. My suggestion is that you ask this question of the mocking framework author(s) instead (by the syntax, I'm guessing you're probably using Moq, whose project is here: https://github.com/moq/moq4 |
Beta Was this translation helpful? Give feedback.
This isn't strictly speaking a question for xUnit.net, since we don't provide a mocking framework. My suggestion is that you ask this question of the mocking framework author(s) instead (by the syntax, I'm guessing you're probably using Moq, whose project is here: https://github.com/moq/moq4