Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何使用FakeiTasy在WebApi控制器上执行集成测试?_Unit Testing_Asp.net Web Api_Nunit_Integration Testing_Fakeiteasy - Fatal编程技术网

Unit testing 如何使用FakeiTasy在WebApi控制器上执行集成测试?

Unit testing 如何使用FakeiTasy在WebApi控制器上执行集成测试?,unit-testing,asp.net-web-api,nunit,integration-testing,fakeiteasy,Unit Testing,Asp.net Web Api,Nunit,Integration Testing,Fakeiteasy,我不擅长实现单元测试和集成测试。我正在尝试为我的应用程序编写一些集成测试。 下面是我的应用程序中的代码片段,让您了解我的代码。 如果你能为我提供一些指导,那将是很大的帮助 namespace MyApplication.ApiControllers { [Authorize] [RoutePrefix("api/customers")] [AppExceptionFilter] public class CustomersController : ApiContr

我不擅长实现单元测试和集成测试。我正在尝试为我的应用程序编写一些集成测试。 下面是我的应用程序中的代码片段,让您了解我的代码。 如果你能为我提供一些指导,那将是很大的帮助

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);
        }
    }
}
namespace MyApplication.ApiControllers
{
[授权]
[RoutePrefix(“api/客户”)]
[AppExceptionFilter]
公共类CustomerController:ApicController
{
专用只读IMediator\u中介;
公共CustomerController(IMediator中介)
{
_调解人=调解人;
}
[HttpGet]
[路线(“GetCustomer”)]
公共异步任务GetCustomer(字符串customerNumber,字符串customerType=null)
{
var result=wait_mediator.RequestAsync(新的GetCustomerRequest(customerNumber,customerType));
返回Ok(结果);
}
}
}
下面是GetCustomerRequest处理程序的实现

public async Task<List<Customer>> HandleAsync(GetCustomerRequest request)
{
    var result = await customerService.GetCustomer(request.CustomerNumber, request.CustomerType);
    // some business logic 
    return result;
}
public异步任务HandleAsync(GetCustomerRequest请求)
{
var result=await customerService.GetCustomer(request.CustomerNumber,request.CustomerType);
//一些商业逻辑
返回结果;
}
下面是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;
}
公共异步任务GetCustomer(字符串customerNumber,字符串customerType=null) { 使用(var dataContext=\u dataContextFactory.Invoke()) { 结果=等待dataContext.Customers .其中(b=>b.CustomerNumber==CustomerNumber) .其中(b=>b.CustomerType==CustomerType) .选择(b=>新客户 { //属性分配。。。 }) .ToListAsync(); } 返回结果; } 下面是我尝试过的集成单元测试

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);
        }
    }
}
namespace MyApplication.Tests.Integrations
{
[测试夹具]
公共类CustomerControllerTests
{
专用字符串\u基地址;
私有字符串\u用户名;
私有字符串\u密码;
私人IapClient(私人IapClient);
[设置]
公共作废设置()
{
_基本地址=”https://mywebaaplication.com“;//要从配置中获取此信息
_用户名=”;
_密码=”;
_apiClient=new apiClient(new apiclientuthenticationhandler(),_baseAddress);//替换为AzureADapiclientuthenticationhandler
}
[测试]
公共异步任务CustomerController_GetCustomer()
{
var customerNumber=string.Empty;
var customerType=500;
var result=await_apiClient.GetAsync($”/api/customers/GetCustomer?customerNumber={customerNumber}&customerType={customerType}”);
Assert.IsNotNull(结果);
Assert.IsTrue(结果?.Length>0);
}
}
}

您可以做几件事:

  • 在单元测试中创建一个webhost,然后对其执行http请求
  • 不是在单元测试中测试控制器,而是在活动性/就绪性检查中测试控制器(因为它只是粘合代码)。只需对您的服务进行集成测试
  • 只需针对“新CustomerController”进行测试

  • 这里没有正确/错误的答案。您只需查看风险,并进行相应的测试。还取决于预期的代码更改类型。有时,只在新更改的上下文中创建测试是很好的,不需要预测所有内容

    你到底有什么问题?