如何使用 FakeItEasy 在 WebApi 控制器上执行集成测试?
How do I perform integration test on WebApi controller using FakeItEasy?
我不熟悉实施单元测试和集成测试。我正在尝试为我的应用程序编写一些集成测试。
以下是我的应用程序中的代码片段,让您了解我的代码。
如果您能为我提供一些指导,那将是很大的帮助。
namespace MyApplication.ApiControllers
{
[Authorize]
[RoutePrefix("api/customers")]
[AppExceptionFilter]
public class CustomersController : ApiController
{
private readonly IMediator _mediator;
public CustomersController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet]
[Route("GetCustomer")]
public async Task<IHttpActionResult> GetCustomer(string customerNumber, string customerType = null)
{
var result = await _mediator.RequestAsync(new GetCustomerRequest(customerNumber, customerType));
return Ok(result);
}
}
}
以下是 GetCustomerRequest 处理程序的实现
public async Task<List<Customer>> HandleAsync(GetCustomerRequest request)
{
var result = await customerService.GetCustomer(request.CustomerNumber, request.CustomerType);
// some business logic
return result;
}
以下是 customerService 的实现
public async Task<List<Customer>> GetCustomer(string customerNumber, string customerType = null)
{
using (var dataContext = _dataContextFactory.Invoke())
{
result = await dataContext.Customers
.Where(b => b.CustomerNumber == customerNumber)
.Where(b => b.CustomerType == customerType)
.Select(b => new Customer
{
// Properties assignment...
})
.ToListAsync();
}
return result;
}
下面是我试过的集成单元测试。
namespace MyApplication.Tests.Integrations
{
[TestFixture]
public class CustomersControllerTests
{
private string _baseAddress;
private string _username;
private string _password;
private IApiClient _apiClient;
[SetUp]
public void Setup()
{
_baseAddress = "https://mywebaaplication.com"; // TODO get this from a config
_username = "";
_password = "";
_apiClient = new ApiClient(new ApiClientAuthenticationHandler(), _baseAddress); // REPLACE with AzureADApiClientAuthenticationHandler
}
[Test]
public async Task CustomersController_GetCustomer()
{
var customerNumber = string.Empty;
var customerType = 500;
var result = await _apiClient.GetAsync<Customer[]>($"/api/customers/GetCustomer?customerNumber={customerNumber}&customerType={customerType}");
Assert.IsNotNull(result);
Assert.IsTrue(result?.Length > 0);
}
}
}
您可以做几件事:
- 在您的单元测试中创建一个虚拟主机,然后对其执行 http 请求
- 不是在单元测试中测试您的控制器,而是在 liveness/readiness 检查中(因为它只是粘合代码)。只需为您的服务进行集成测试。
- 只需针对 "new CustomersController"
进行测试
此处没有 right/wrong 答案。您只需查看风险,并进行相应的测试。还取决于您期望的代码更改类型。有时只在新变化的上下文中创建测试就很好,不需要预测一切。
我不熟悉实施单元测试和集成测试。我正在尝试为我的应用程序编写一些集成测试。 以下是我的应用程序中的代码片段,让您了解我的代码。 如果您能为我提供一些指导,那将是很大的帮助。
namespace MyApplication.ApiControllers
{
[Authorize]
[RoutePrefix("api/customers")]
[AppExceptionFilter]
public class CustomersController : ApiController
{
private readonly IMediator _mediator;
public CustomersController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet]
[Route("GetCustomer")]
public async Task<IHttpActionResult> GetCustomer(string customerNumber, string customerType = null)
{
var result = await _mediator.RequestAsync(new GetCustomerRequest(customerNumber, customerType));
return Ok(result);
}
}
}
以下是 GetCustomerRequest 处理程序的实现
public async Task<List<Customer>> HandleAsync(GetCustomerRequest request)
{
var result = await customerService.GetCustomer(request.CustomerNumber, request.CustomerType);
// some business logic
return result;
}
以下是 customerService 的实现
public async Task<List<Customer>> GetCustomer(string customerNumber, string customerType = null)
{
using (var dataContext = _dataContextFactory.Invoke())
{
result = await dataContext.Customers
.Where(b => b.CustomerNumber == customerNumber)
.Where(b => b.CustomerType == customerType)
.Select(b => new Customer
{
// Properties assignment...
})
.ToListAsync();
}
return result;
}
下面是我试过的集成单元测试。
namespace MyApplication.Tests.Integrations
{
[TestFixture]
public class CustomersControllerTests
{
private string _baseAddress;
private string _username;
private string _password;
private IApiClient _apiClient;
[SetUp]
public void Setup()
{
_baseAddress = "https://mywebaaplication.com"; // TODO get this from a config
_username = "";
_password = "";
_apiClient = new ApiClient(new ApiClientAuthenticationHandler(), _baseAddress); // REPLACE with AzureADApiClientAuthenticationHandler
}
[Test]
public async Task CustomersController_GetCustomer()
{
var customerNumber = string.Empty;
var customerType = 500;
var result = await _apiClient.GetAsync<Customer[]>($"/api/customers/GetCustomer?customerNumber={customerNumber}&customerType={customerType}");
Assert.IsNotNull(result);
Assert.IsTrue(result?.Length > 0);
}
}
}
您可以做几件事:
- 在您的单元测试中创建一个虚拟主机,然后对其执行 http 请求
- 不是在单元测试中测试您的控制器,而是在 liveness/readiness 检查中(因为它只是粘合代码)。只需为您的服务进行集成测试。
- 只需针对 "new CustomersController" 进行测试
此处没有 right/wrong 答案。您只需查看风险,并进行相应的测试。还取决于您期望的代码更改类型。有时只在新变化的上下文中创建测试就很好,不需要预测一切。