-1

Unit testing on MVC vb.net project. One of the action method in my controller, I was getting an error on Request.Params["FieldName"]

In this case, how to mock: Request.Params["FieldName"] in unit test method? Prefer VB.net, and c# is accepted too.

    Dim httpRequest = New Mock(Of System.Web.HttpRequestBase)()
    Dim httpContext = New Mock(Of System.Web.HttpContextBase)()

    Dim parameters As New NameValueCollection()
    parameters("YourField") = "testField"

    httpRequest.Setup(Function(x) x.Params).Returns(parameters)
    httpContext.Setup(Function(x) x.Request).Returns(httpRequest.[Object])
Julia
  • 83
  • 1
  • 11
  • Possible duplicate of [How to mock the Request on Controller in ASP.Net MVC?](https://stackoverflow.com/questions/970198/how-to-mock-the-request-on-controller-in-asp-net-mvc) – Fabio Apr 22 '18 at 20:56

1 Answers1

0

You need to mock the request and the context. In the request, you need to mock the Params["YourField"] as well but the problem is that this indexer is not virtual so you will not be able to mock it. Instead you can mock Get method. Here is how to do so with Moq:

var request = new Mock<HttpRequestBase>();
// We are mocking the Get method
request.Setup(x => x.Params.Get("FieldName")).Returns("whatever...");

var context = new Mock<HttpContextBase>();
context.Setup(x => x.Request).Returns(request.Object);

In your code, you will need to use Get instead of the indexer (You cannot use Params[""]).

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • This is the perfect solution. Thanks CodingYoshi. But I got a syntax error in VB, do you know why? https://www.screencast.com/t/LUlQKnd7JLTN – Julia Apr 24 '18 at 13:39
  • @Julia That is my fault. It should be `Setup` not, `SetupGet`...probably auto-correct on my phone. I have edited and fixed the answer. – CodingYoshi Apr 24 '18 at 13:46
  • Thanks. I got the correct syntax, but I got another runtime error, do you know the reason? https://www.screencast.com/t/XiZIfO4Xam – Julia Apr 24 '18 at 14:11
  • @Julia See my edit please. – CodingYoshi Apr 24 '18 at 14:35
  • IIt has no exception now, but in my controller method, I need to request parameter value: – Julia Apr 24 '18 at 15:04
  • Public Function SaveReord() Dim FieldNo As Integer FieldNo = Convert.ToInt32(Request.Params("FieldName")) ... End Function – Julia Apr 24 '18 at 15:04
  • That is, I couldn't get Request.Params value there. – Julia Apr 24 '18 at 15:07
  • 1
    @julia read my answer carefully please. You cannot just copy paste my code and if it does not work, message me. You need to read your code, the error and reread the answer before you message me. You cannot use `Request.Params("FieldName")` as I explained in the answer. Use `Get("FieldName")` instead. – CodingYoshi Apr 24 '18 at 15:43
  • It's fixed by using NameValueCollection property to be able to get request params. Thanks very much for your help. – Julia Apr 24 '18 at 16:24
  • @julia just to confirm: you used `Request.Params.Get("FieldName")` in your code, right? – CodingYoshi Apr 24 '18 at 17:41
  • see my edit above, I can get Request.Params("FieldName") value by using NameValueCollection in HttpRequest setup. – Julia Apr 26 '18 at 04:22