Question

Writting some unit tests and im not finding a way to moq out a useragent string using mvc3, moq, and mvccontrib. Any ideas how to mock the useragent string with moq and mvccontrib?

ive been tring to do this but the useragent property is read only.

 public static RouteController GetRouteController()
    {
        var controller = new RouteController();
        var builder = new TestControllerBuilder();

        builder.HttpContext.Request.UserAgent = "foo-agent";
        //mocks up the asp pipeline (http context, asp session, etc)
        builder.InitializeController(controller);


        return controller;
    }
Was it helpful?

Solution

ok found how to do it. Just needed to use a rhinomocks stub on the request. This works for me.

public static RouteController GetRouteController(string useragent)
        {
            var controller = new RouteController();
            var builder = new TestControllerBuilder();

            builder.InitializeController(controller);
            //mock out the useragent string
            controller.HttpContext.Request.Stub(r => r.UserAgent).Return(useragent);
            return controller;
        }

Hope this helps someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top