Reputation: 14148
I am using the hanselman tutorial to use Moq to create unit tests for methods that take httpcontext as input parameter.
public static class MvcMockHelpers
{
public static HttpContextBase FakeHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
return context.Object;
}
public static HttpContextBase FakeHttpContext(string url)
{
var context = FakeHttpContext();
context.Request.SetupRequestUrl(url); <---- error here
return context;
}
}
I am getting the following error on context.Request.SetupRequestUrl(url);
Error 1 'System.Web.HttpRequestBase' does not contain a definition for 'SetupRequestUrl' and no extension method 'SetupRequestUrl' accepting a first argument of type 'System.Web.HttpRequestBase' could be found (are you missing a using directive or an assembly reference?)
Please help.
I am trying to find a very simple easy to follow tutorial on setting up httpcontext request object and pass into method with context input parameter and be able to create unit test but all the examples in google either do not work or are complicated. please help.
Upvotes: 0
Views: 1611
Reputation: 1038730
Make sure you have declared the SetupRequestUrl
extension method inside the MvcMockHelpers
class as shown by the Haacked:
public static void SetupRequestUrl(this HttpRequestBase request, string url)
{
...
}
You should have copy-pasted the entire code fragment from the article.
Upvotes: 3