Как имитировать HttpContext.GetServerVariable() в .NET Core ⇐ C#

Место общения программистов C#
Anonymous
Как имитировать HttpContext.GetServerVariable() в .NET Core

Сообщение Anonymous »

Я пытаюсь протестировать приведенный ниже метод с помощью NUnit и Moq в среде .NET Core:

Код: Выделить всё

[HttpGet]
public async Task DeviceType()
{
string deviceIp = HttpContext.GetServerVariable("REMOTE_ADDR");
var result = await DeviceService.GetTypeForAsync(deviceIp);
return Ok(result);
}
Проблема заключается в имитации HttpContext.GetServerVariable. Я попытался имитировать контекст и вставить его с помощью ControllerContext:

Код: Выделить всё

var actionContext = new Mock();
var httpContext = new Mock();
httpContext.Setup(x => x.GetServerVariable(It.IsAny())).Returns("192.168.1.1");
actionContext.Setup(x => x.HttpContext).Returns(httpContext.Object);
//...
var deviceController = new DeviceController();
deviceController.ControllerContext = new ControllerContext(actionContext.Object);
Но тест не пройден и выдает ошибку:

System.NotSupportedException: Неподдерживаемое выражение: x => x. GetServerVariable(It.IsAny())
Методы расширения (здесь: HttpContextServerVariableExtensions.GetServerVariable) не могут использоваться в выражениях настройки/проверки.

Вопрос в том, как издеваться над HttpContext.GetServerVariable("REMOTE_ADDR")?

Подробнее здесь: https://stackoverflow.com/questions/652 ... n-net-core

Вернуться в «C#»