jeudi 26 novembre 2015

How to Mock/Stub or simply ignore HttpRequest when unit testing

public class DemoController : Controller { private readonly ICommonOperationsRepository _commonRepo; public DemoController (ICommonOperationsRepository commonRepo) { _commonRepo = commonRepo; }

    public ActionResult Default()
    {
        var model = new DemoModel();
        try
        {
            **DeviceDetection dd = new DeviceDetection(Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString());
            dd.DetectDevice();**

            model.ListTopListing.AddRange(_commonRepo.GetListings());
        }
        catch (Exception ex)
        {
            ExceptionHandler objErr = new ExceptionHandler(ex, "DemoController .Default()\n Exception : " + ex.Message);
            objErr.LogException();
        }
        return View(model);
    }

Problem:The DeviceDetection has concrete dependency here so I can't unit test my controller. I don't want to Mock the Http request since I just want to test the controller and not DeviceDetection module.

How can i mock/avoid accessing this (Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString()) which is causing all the problems.

Aucun commentaire:

Enregistrer un commentaire