Maybe there are some sort of permissions I need to set up in the test webhost ? The Get and Post for the tests work fine. But get a HTTP 405 error when it tries to call the DELETE method on the controller.
[HttpDelete("{id:int}")]
public async Task<ActionResult<RfAttachmentModel>> DeleteByIdAsync(int id)
{
await _rfAttachmentService.DeleteByIdAsync(id);
return NoContent();
}
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("LocalTesting");
builder.ConfigureServices(services =>
{
services.AddEntityFrameworkInMemoryDatabase();
ServiceProvider provider = services
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
services.AddDbContext<PwdrsContext>(options =>
{
options.UseInMemoryDatabase("Pwdrs");
options.UseInternalServiceProvider(provider);
});
ServiceProvider sp = services.BuildServiceProvider();
using (IServiceScope scope = sp.CreateScope())
{
IServiceProvider scopedServices = scope.ServiceProvider;
PwdrsContext db = scopedServices.GetRequiredService<PwdrsContext>();
ILoggerFactory loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>();
ILogger<CustomWebApplicationFactory<TStartup>> logger = scopedServices
.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
try
{
PwdrsContextSeed.SeedAsync(db, loggerFactory).Wait();
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the " +
"database with test messages. Error: {ex.Message}");
}
}
});
}
}
EDIT1 : Here is the method in the test project that makes the call
[Fact]
public async Task Delete_Item_By_Id()
{
HttpResponseMessage responseDelete = await Client.GetAsync("/api/RfAttachment/DeleteById/1");
responseDelete.EnsureSuccessStatusCode();
HttpResponseMessage responseGetAll = await Client.GetAsync("/api/RfAttachment/GetAll");
responseGetAll.EnsureSuccessStatusCode();
string stringResponse = await responseGetAll.Content.ReadAsStringAsync();
List<RfAttachment> result = JsonConvert
.DeserializeObject<IEnumerable<RfAttachment>>(stringResponse)
.ToList();
Assert.Single(result);
}