I'm trying to create a Moq test project but I cannot inject the dependencies on the service/interface because my service constructor doesn't have an interface argument.
Service constructor:
public NearEarthObjectService(HttpClient httpClient)
{
_httpClient = httpClient;
}
Project Program.cs:
using NasaApi.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri("https://api.nasa.gov/neo/rest/v1/") });
builder.Services.AddScoped<INearEarthObjectService, NearEarthObjectService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Empty Moq test project :(
public class NearEarthObjectService_Tests {
public NearEarthObjectService_Tests()
{
}
As can you see, the dependencies in the program are injected in the Startup.cs with the builder.Services.AddScoped
builder.Services.AddScoped<INearEarthObjectService, NearEarthObjectService>();
My controller
[Route("[controller]")]
[ApiController]
public class AsteroidsController : ControllerBase
{
INearEarthObjectService _earthObjectService;
public AsteroidsController(INearEarthObjectService earthObjectService)
{
_earthObjectService = earthObjectService;
}
// GET: <AsteroidsController>
[HttpGet]
public async Task<ActionResult<IEnumerable<NearEarthObjectDTO>>> Get([BindRequired, FromQuery] int days)
{
if (days < 1 || days > 7)
{
return StatusCode(400);
}
else
{
var response = await Task.Run(() => _earthObjectService.GetAllNeos(days));
return Ok(response);
}
}
}
Thanks to all!!