Ejemplo n.º 1
0
        public void CurrentUrlAndCurrentAbsoluteUrl_UrlHelperRequestContextIsNull_ThrowsException()
        {
            var urlHelper = new UrlHelper();
            // It is not possible to set UrlHelper.RequestContext to null and at the same time to have RouteCollection being not null.
            // Therefor this little trick to pass the null check for the urlHelper.RouteCollection.

            urlHelper.GetType().GetProperty("RouteCollection").GetSetMethod(true).Invoke(urlHelper, new object[] { new RouteCollection() });
            Assert.That(urlHelper.RouteCollection, Is.Not.Null);

            var parameterName = Assert.Throws<ArgumentNullException>(() => urlHelper.CurrentUrl(new Func<object, object>[0])).ParamName;
            Assert.That(parameterName, Is.EqualTo("urlHelper.RequestContext"));

            parameterName = Assert.Throws<ArgumentNullException>(() => urlHelper.CurrentAbsoluteUrl(Protocol.Http, new Func<object, object>[0])).ParamName;
            Assert.That(parameterName, Is.EqualTo("urlHelper.RequestContext"));
        }
Ejemplo n.º 2
0
        public void CurrentUrlAndCurrentAbsoluteUrl_NewRouteAndQueryStringParametersIsNull_ThrowsException()
        {
            var urlHelper = new UrlHelper(new RequestContext(), new RouteCollection());

            var parameterName = Assert.Throws<ArgumentNullException>(() => urlHelper.CurrentUrl(null)).ParamName;
            Assert.That(parameterName, Is.EqualTo("newRouteAndQueryStringParameters"));

            parameterName = Assert.Throws<ArgumentNullException>(() => urlHelper.CurrentAbsoluteUrl(Protocol.Http, null)).ParamName;
            Assert.That(parameterName, Is.EqualTo("newRouteAndQueryStringParameters"));
        }