The usual erroneous error “the name httpsRequest” does not exist in its current context to make httpRequestMessage global

I am trying to use Specflow to automate an API and I have to admit you spend more time in wrapping your code around the template of the step definition file rather than simply writing the code. Anyway, here goes, in the then method I want the httpRequestMessage to be global so I can catch it within the last step accept the best answer I found was a 2 liner….. too good to be true right, hence I get an error. So as global goes under my public class I have the following 2 lines:

HttpRequestMessageFeature hreqmf = new HttpRequestMessageFeature(httpRequest.HttpContext);

HttpRequestMessage httpRequestMessage = hreqmf.HttpRequestMessage;

For the httpRequest in brackets says it doesn’t exist in its current context while the hreqmf.HttpRequestMessage. The hreqmf says a field initializer cannot reference the non static field, method or property.

Can someone enlighten me in where I am going wrong. For completion I have put the last step of the step definition file below:

 [Then(@"the status code of the reponse should be (.*)")]
        public async Task ThenTheStatusCodeOfTheReponseShouldBeExpectedResultAsync(string statusCode)
        {
            var response = await client.SendAsync(httpRequestMessage);
            Assert.That(response.StatusCode.ToString(), Is.EqualTo(statusCode));
        }

  • Please edit your question to include more of the code, and the exact error message you are receiving.

    – 

Leave a Comment