dimanche 28 août 2016

Error on mocking Request object for mvc controller

I am getting below error when i am trying to unit test below controller action method which is using request object. (Moq 4.5.8.0 version is used)

 public class paymentController
    {
        public void actionMethod()
        {
             Payment.BaseAmount = Convert.ToDecimal(Request["Amount"]);
             Payment.Amount = Convert.ToDecimal(Request["chargeTotalValue"]);
        }
    }

Below is my unit test case.

public void TestMethod() {

       var request = new Mock<HttpRequestBase>();
        request.SetupGet(rq => rq["Amount"]).Returns("100");
        request.SetupGet(rq => rq["chargeTotalValue"]).Returns("200");
        request.SetupGet(rq => rq["feeValue"]).Returns("20");

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

        controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);

After i run Test case getting below error :

Type INitialization exception was handled by user code.

An exception of type 'System.TypeInitializationException' occurred in Moq.dll but was not handled in user code

Additional information: The type initializer for 'Moq.Mock`1' threw an exception.

Object (Moq.Mock) = '((Moq.Mock)request).Object' threw an exception of type 'System.TypeInitializationException'

Please give me some suggestions on this problem.

Aucun commentaire:

Enregistrer un commentaire