I'm trying to test my API with Moq but I'm having trouble with the mocked service and the execution of the test.
Unit test code:
public class UnitTest1
{
private readonly INearEarthObjectService _repo;
Mock<INearEarthObjectService> _mockRepo = new Mock<INearEarthObjectService>();
public UnitTest1()
{
var services = new ServiceCollection();
services.AddTransient<INearEarthObjectService, NearEarthObjectService>();
services.AddTransient(sp => new HttpClient { BaseAddress = new Uri("https://api.nasa.gov/neo/rest/v1/") });
var serviceProvider = services.BuildServiceProvider();
_repo = serviceProvider.GetService<INearEarthObjectService>();
}
[Fact]
public async void Test1()
{
_mockRepo.Setup(p => p.GetAllNeos(2)).Returns(Task.FromResult<IEnumerable<NearEarthObjectDTO>>).Verifiable();
var result = _mockRepo.Object.GetAllNeos(2);
Assert.True(result.IsCompletedSuccessfully);
}
}
I'm having trouble in this line:
var result = _mockRepo.Object.GetAllNeos(2);
I gives me this exception:
System.ArgumentException: 'Object of type 'System.Int32' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[NasaApi.Models.NearEarthObjectDTO]'.'
My Service code if it is useful:
public async Task<IEnumerable<NearEarthObjectDTO>> GetAllNeos(int days)
{
//Variable declaration
HttpClient client = new HttpClient();
DateTime today = DateTime.Now;
DateTime nextday = today.AddDays(days);
List<NearEarthObjectDTO> list = new List<NearEarthObjectDTO>();
NearEarthObjectDTO neo;
//URL parsing & data request
var complete_url = url start_date_param today.Date.ToString(
"yyyy-MM-dd") "&" end_date_param nextday.Date.ToString("yyyy-MM-dd") "&" detailed_param "&" api_key_param;
var feed = await client.GetFromJsonAsync<Rootobject>(complete_url);
//Data filtering and parsing into DTO
if (feed != null)
{
foreach (var register in feed.near_earth_objects)
{
foreach(var item in register.Value)
{
neo = new NearEarthObjectDTO();
neo.Id = item.id;
neo.Nombre = item.name;
neo.Fecha = item.close_approach_data[0].close_approach_date;
neo.Velocidad = item.close_approach_data[0].relative_velocity.kilometers_per_hour;
neo.Diametro = DiameterCalc.Calc(item.estimated_diameter.meters.estimated_diameter_min,
item.estimated_diameter.meters.estimated_diameter_min);
neo.Planeta = item.close_approach_data[0].orbiting_body;
if (item.is_potentially_hazardous_asteroid && neo.Planeta.Equals("Earth") && !list.Contains(neo))
{
list.Add(neo);
}
}
}
}
//Return just the top 3 biggest potentially hazardous asteroids
return list.OrderByDescending(x => x.Diametro).Take(3);
}
I don't know where is the parsing problem because I'm a using a var which is supposed to adapt to any type.
CodePudding user response:
You don't test the mock object. Your mock setup should be something like
_mockRepo.Setup(p => p.GetAllNeos(It.IsAny<int>)).ReturnsAsync(new List<NearEarthObjectDTO>()).Verifiable();
Here you are saying when GetAllNeos() gets called with any int
it should return my List
(which would be some fake data that the GetAllNeos() function is to return)