Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock method with Expression as parameter. #1324

Closed
balajis-13395 opened this issue Jan 12, 2023 · 1 comment
Closed

Mock method with Expression as parameter. #1324

balajis-13395 opened this issue Jan 12, 2023 · 1 comment

Comments

@balajis-13395
Copy link

Hi,
I have the to mock Method with expression or predicate as parameter

I need to mock the this.catalogueRepository.GetAsync(predicate).ConfigureAwait(false); in the below method

public async Task 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

}

@stakx
Copy link
Contributor

stakx commented Jan 12, 2023

This issue tracker focuses on Moq development. This issue doesn't look like a bug report, nor like a feature request, but like a simple usage question. Places like Stack Overflow are perhaps better suited for such questions as they will reach a larger audience.

@stakx stakx closed this as completed Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants