2013-03-14 77 views
1

寫一些單元測試,並沒有找到一種方法來moq出使用mvc3,moq和mvccontrib useragent字符串。任何想法如何嘲笑與moq和mvccontrib useragent字符串?mvc3 moq mvccontrib如何moq useragent?

我一直在做這個,但useragent屬性是隻讀的。

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; 
    } 

回答

1

好的發現如何做到這一點。只需要在請求中使用rhinomocks存根。這對我有用。

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; 
     } 

希望這可以幫助別人。