0
Hello, I have a method that uses a read Repository for a holiday table, in this case... I need to create a unit test using xUnit Follow the excerpt of my method:
[HttpGet]
[ProducesResponseType(typeof(HolidayViewModel), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
public async Task<IActionResult> GetAll()
{
var data = await _readRepository.FindAll()
.ToListAsync()
.ConfigureAwait(false);
if (data == null || data.Count == 0)
return NoContent();
return Ok(data.ToViewModel());
}
In the case the previous test was the following
private readonly Mock<IReadRepository<Holiday>> _repository = new Mock<IReadRepository<Holiday>>();
[Fact]
public async System.Threading.Tasks.Task Should_return_sucess_when_getting_holidays()
{
var mock = HolidayListFactory().AsQueryable().BuildMock();
_repository.Setup(x => x.FindAll()).Returns(mock.Object);
var controller = new HolidaysController(_repository.Object);
var response = await controller.GetAll().ConfigureAwait(false) as OkObjectResult;
Assert.Equal(response.StatusCode, (int)HttpStatusCode.OK);
}
public List<Holiday> HolidayListFactory()
{
return new List<Holiday>()
{
new Holiday()
{
Id = Guid.NewGuid(),
HolidayDate = DateTime.UtcNow,
HolidayName = "Holiday 1",
CreatedBy = "Yuri",
CreatedOn = DateTime.UtcNow,
CompanyId = Guid.NewGuid()
},
new Holiday()
{
Id = Guid.NewGuid(),
HolidayDate = DateTime.UtcNow,
HolidayName = "Holiday 2",
CreatedBy = "Yuri",
CreatedOn = DateTime.UtcNow,
CompanyId = Guid.NewGuid()
},
new Holiday()
{
Id = Guid.NewGuid(),
HolidayDate = DateTime.UtcNow,
HolidayName = "Holiday 3",
CreatedBy = "Yuri",
CreatedOn = DateTime.UtcNow,
CompanyId = Guid.NewGuid()
}
};
}
However what was used in Buildmock now is no longer functional, I would like to know how I could test my method differently to this, could someone please help me?
I can’t understand your doubt.
– Jéf Bueno
In case the test I put up no longer works in the version . net core 3.0 and I would like to know another way to test by passing a mock to my Pository
– Yuri Gomes