Reputation: 125
My mock setup are like below
public class LicensePathControllerTest
{
private MockRepository mockRepository;
}
public LicensePathControllerTest()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);
this.mockIJobLicensePathService =
this.mockRepository.Create<IJobLicensePathService>();
this.mockLog = this.mockRepository.Create<ILog>();
}
private LicensePathController CreateLicensePathController()
{
return new LicensePathController(
this.mockIJobLicensePathService.Object,this.mockLog.Object);
}
My method is
public async Task<IActionResult> GetLicensePath([FromBody] PathSearch pathSearch)
{
var (paths, pagination) = await _pathService.GetJobPathByFilters(pathSearch); //need to mock this line.
return ok(new OkResponse<IList<JobLicensePath>>(licensePaths, pagination.PageNumber, pagination.PageSize, pagination.TotalPages, pagination.TotalCount));
}
my service class method returning as well
public async Task<(IList<JobLicensePath>, PaginationModel)> GetJobPathByFilters(PathSearch pathSearch)
{
//...
IEnumerable<JobLicensePath> objJobLicensePath = null;
objJobLicensePath = await _baseRepository.GetAsync<JobLicensePath>(filter: expression, null, null, skip: skipVal, take: take, false);
return (objJobPath.ToList(), new PaginationModel(pageNumber, pageSize, totalCount));
}
I am trying to mock like below few trials but which all are not setup.
First Try
mockIJobLicensePathService
.Setup(x => x.GetJobPathByFilters(It.IsAny<PathSearch>()))
.Returns(Task.FromResult(It.IsAny<Task(IList<JobLicensePath>, PaginationModel)>()));
The type arguments for method 'Task.FromResult(TResult)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Second Try
mockIJobLicensePathService
.Setup(x => x.GetJobPathByFilters(It.IsAny<PathSearch>()))
.Returns(Task.FromResult(It.IsAny<IList<JobLicensePath>, PaginationModel>()>));
Using the generic method 'It.IsAny()' requires 1 type arguments
mockIJobLicensePathService
.Setup(x => x.GetJobPathByFilters(It.IsAny<PathSearch>()))
.Returns(Task.FromResult(It.IsAny<IList<JobLicensePath>>(), It.IsAny<PaginationModel>()));
No overload for method 'FromResult' takes 2 arguments
Upvotes: 1
Views: 769
Reputation: 22704
The methods of the It
class, like Is
or IsAny
, should be used only inside the Setup
method.
They help you specify when your mock should "trigger".
If your method returns with a Task
then you have several options
ReturnAsync
var expectedJobLicensePaths = ...;
var expectedPaginationModel = ...;
mockIJobLicensePathService
.Setup(x => x.GetJobPathByFilters(It.IsAny<PathSearch>()))
.ReturnsAsync((expectedJobLicensePaths, expectedPaginationModel));
Result
+ Return
Since 4.16 you can rewrite the above setup to this
var expectedJobLicensePaths = ...;
var expectedPaginationModel = ...;
mockIJobLicensePathService
.Setup(x => x.GetJobPathByFilters(It.IsAny<PathSearch>()).Result)
.Returns((expectedJobLicensePaths, expectedPaginationModel));
Upvotes: 0
Reputation: 10839
You should use ReturnsAsync method of Moq as shown below.
// arrange
// Create the dummy object or use the existing one if already exists as part of your mock setup.
IList<JobLicensePath> jobLicensePath = new List<JobLicensePath>();
PaginationModel paginationModel = new PaginationModel();
mockIJobLicensePathService
.Setup(x => x.GetJobPathByFilters(It.IsAny<PathSearch>()))
.ReturnsAsync((jobLicensePath, paginationModel));
Upvotes: 0
Reputation: 325
It.IsAny is used to check the method's parameter. You can mock it like this:
// arrange
(IList<JobLicensePath>, PaginationModel) result = (new List<JobLicensePath>(), new PaginationModel());
var mock = new Mock<IPathService>();
mock
.Setup(s => s.GetJobPathByFilters(It.IsAny<PathSearch>()))
.Returns(Task.FromResult(result));
Upvotes: 0