C# Mock Request.Browser.MajorVersion using RhinoMocks

时间:2017-12-18 07:40:58

标签: c# asp.net-mvc rhino-mocks

I have the following code in an MVC controller:

var table_element = $(this).prev('table.w2bw2c-with-artists-table');
var new_with_artist_index = $(table_element).rowCount();

I need to stub out the second part using Rhino Mocks. I have tried this:

        var isIe6 = Request.Browser.Type.ToUpper().Contains("IE") && Request.Browser.MajorVersion < 7;

The above code returns 0 for the HttpRequest.Stub(a => a.Browser).Return(new HttpBrowserCapabilitiesWrapper(new HttpBrowserCapabilities { Capabilities = new Dictionary<string, string> { {"majorversion", "11"}, {"MajorVersion", "11"}, {"browser", "IE"}, {"isMobileDevice","false"} } })); in the code. I have also tried this:

MajorVersion

Which just throws an exception on setup saying Browser needs to be set. I've also tried it in combination with the HttpRequest.Stub(a => a.Browser.MajorVersion).Return(11); above, but it still says Browser needs to be set.

How do I stub/mock out Request.Browser.MajorVersion in MVC?

Some test code if anyone needs it:

HttpBrowserCapabilitiesWrapper

Method under test in BasketController:

    private BasketController sut;
    protected HttpRequestBase HttpRequest;

    [Test]
    public void ShowsMajorVersionDoesNotWork()
    {
        // Arrange
        HttpRequest.Stub(a => a.Browser).Return(new HttpBrowserCapabilitiesWrapper(new HttpBrowserCapabilities
        {
            Capabilities = new Dictionary<string, string>
            {
                {"majorversion", "11"},
                {"MajorVersion", "11"},
                {"browser", "IE"},
                {"isMobileDevice","false"}
            }
        }));
        //HttpRequest.Stub(a => a.Browser.MajorVersion).Return(11); // Throws Exception
        sut = new BasketController()
        {
            Url = new UrlHelper(new RequestContext(HttpContext, new RouteData()), new RouteCollection())
        };
        sut.ControllerContext = new ControllerContext(HttpContext, new RouteData(), sut);


        // Act
        var result = sut.Method();

        // Assert
        Assert.That(result, Is.EqualTo("11")); // Fails, always returns 0
    }

1 个答案:

答案 0 :(得分:2)

您可以使用HttpBrowserCapabilities创建BrowserCapabilitiesFactory(如here所述),使用IE11 user-agent string,同时您可以使用MajorVersion模拟任何其他浏览器public HttpRequestBase GetInterenetExplorer11StubRequest() { return GetBrowserCapabilitiesStub("Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"); } public HttpRequestBase GetBrowserCapabilitiesStub(string userAgentString) { var browser = new HttpBrowserCapabilities { Capabilities = new Hashtable { { string.Empty, userAgentString } } }; var factory = new BrowserCapabilitiesFactory(); factory.ConfigureBrowserCapabilities(new NameValueCollection(), browser); var request = MockRepository.GenerateStub<HttpRequestBase>(); request.Stub(a => a.Browser).Return(new HttpBrowserCapabilitiesWrapper(browser)); return request; } 正确的字符串:

Sum(`sales`.`quantity`) AS totquantity,
`transactions`.`price` AS price,
Sum(`sales`.`quantity`)  *  `transactions`.`price` AS grantot
from (`sales` join `transactions` on((`transactions`.`idtransaction` = `sales`.`idtransaction`)))
where ((`sales`.`createon` > '01/01/2017') and (`sales`.`createon` < 'now()'))
group by `sales`.`idtransaction`