Reputation: 622
I have a controller Action as:
[HttpPost]
[EnableQuery]
[ODataRoute("PostData")]
public async Task<string> PostData(HttpRequestMessage message)
{
//Do operations
}
I need to create a mock for this method ,but I am not getting how to pass the parameter "HttpRequestMessage",
because if there was any variable to be passed , then its just initializing with the type like string or int.
How to handle this condition in mock?
Upvotes: 4
Views: 4428
Reputation: 4932
HttpRequestMessage
is very mutable:
public class HttpRequestMessage : IDisposable
{
[... ctors]
public Version Version { get; set; }
public HttpContent Content { get; set; }
public HttpMethod Method { get; set; }
public Uri RequestUri { get; set; }
public HttpRequestHeaders Headers { get; }
public IDictionary<string, object> Properties { get; }
[... Dispose, ToString]
}
So if you just need to set RequestUri
for example, you can:
var requestMessage = new HttpRequestMessage() { RequestUri = new Uri("http://www.google.com") };
yourClassInstance.PostData(requestMessage);
Even if you need to add some headers or properties GetRequestContext
can create HttpRequestContext
from, you can do
requestMessage.Headers.Add("h", "v");
requestMessage.Properties.Add("p", "v");
This design (of HttpRequestMessage
) does not adhere to functional programming principles at all, but at least you can test it easily.
Upvotes: 2