我正在学习创建集成测试。我将Web应用程序工厂配置为使用InMemoryDB,但看来我的请求不起作用。 这是我的自定义Web应用程序工厂:
public class APIWebApplicationFactory<TStartup> : WebApplicationFactory<Startup>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(DbContextOptions<ProjectsDbContext>));
if(descriptor != null)
{
services.Remove(descriptor);
}
services.AddDbContext<ProjectsDbContext>(options =>
{
var id = Guid.NewGuid().ToString();
options.UseInMemoryDatabase(id);
});
});
}
}
这是我的测试:
public WebAPIIntegrationTests(APIWebApplicationFactory<Startup> factory)
{
var builder = new DbContextOptionsBuilder<ProjectsDbContext>();
var id = Guid.NewGuid().ToString();
builder.UseInMemoryDatabase(id);
var options = builder.Options;
ProjectsDbContext context = new ProjectsDbContext(options);
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
_userService = new UserService(new UsersRepository(context));
_taskService = new TaskService(new TasksRepository(context));
_projectService = new ProjectService(new ProjectsRepository(context));
_teamService = new TeamService(new TeamsRepository(context));
_client = factory.CreateClient();
}
[Fact]
public async void CreatingProject()
{
var project = new Project()
{
Name = "Expedita amet quas id a.",
Description = "Ea ab omnis saepe rem vel et.\nIllo quaerat eos accusantium reiciendis dolores quibusdam ratione.",
CreatedAt = DateTime.Now,
Deadline = DateTime.Now,
AuthorId = 29,
TeamId = 8
};
var obj = JsonConvert.SerializeObject(project);
var content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes(obj));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var post = await _client.PostAsync("api/projects", content);
var get = await _client.GetAsync("api/projects/101");
Assert.Equal(HttpStatusCode.OK, get.StatusCode);
get.EnsureSuccessStatusCode();
}
在调试时,我可以看到内存数据库正在运行,但是所有请求都返回“未找到”。
编辑:
我的控制器:
[Route("api/[controller]")]
[ApiController]
public class ProjectsController : ControllerBase
{
private readonly ProjectService _projectService;
private readonly Service _service;
private IMapper _mapper;
public ProjectsController(ProjectService projectService, Service service, IMapper mapper)
{
_service = service;
_projectService = projectService;
_mapper = mapper;
}
[HttpGet]
public ActionResult<List<ProjectDto>> GetAllProjects()
{
var result = _projectService.GetAllProjects();
return _mapper.Map<List<ProjectDto>>(result);
}
[HttpGet("{id}")]
public ActionResult<ProjectDto> GetProject(int id)
{
var result = _projectService.GetProject(id);
return _mapper.Map<ProjectDto>(result);
}
[HttpPost]
public ActionResult<ProjectDto> PostProject(ProjectDto project)
{
var result = _mapper.Map<Project>(project);
_projectService.PostProject(result);
return project;
}
[HttpDelete("{id}")]
public NoContentResult DeleteProject(int id)
{
_projectService.DeleteProject(id);
return NoContent();
}
[HttpPut("{id}")]
public ActionResult<ProjectDto> UpdateProject(ProjectDto project)
{
var result = _mapper.Map<Project>(project);
_projectService.UpdateProject(result);
return project;
}
}