I am adding unit tests to a large old codebase written in C # / ASP.NET / webforms. We use MOQ and XUnit. We managed to make fun of the values โโof the query string using syntax, for example:
Mock<HttpRequestBase> request = new Mock<HttpRequestBase>(); NameValueCollection queryStringParams = new NameValueCollection(); queryStringParams.Add("name", "Fred Jones"); request.Setup(x => x.QueryString).Returns(queryStringParams);
This allows this code to work fine:
string name = _mockRequest.QueryString["name"];
The problem is that there are many calls throughout the code base to get the string variables of the query or form variables in the form:
string name = HttpContext.Current.Request["name"];
The indexer seems to be looking at all the different collections: query strings, form values, cookies, and server variables. I do not want to introduce many potential side effects, refactoring production code to use one of these collections.
Does anyone know a way to mock this index in HttpRequest?
source share