Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created June 1, 2015 20:10
Show Gist options
  • Save christopherbauer/e475a199bd9978283fd8 to your computer and use it in GitHub Desktop.
Save christopherbauer/e475a199bd9978283fd8 to your computer and use it in GitHub Desktop.
Testable, mockable version of a url.content resolver
using System.Web;
using System.Web.Mvc;
namespace KK.Store.DigitalCoupons.Web.Controllers
{
public class ContentResolver : IContentResolver
{
public string Resolve(string imageLocation, HttpRequestBase httpRequestBase)
{
return new UrlHelper(httpRequestBase.RequestContext).Content(imageLocation);
}
}
}
using System.Web;
using System.Web.Routing;
using KK.Store.DigitalCoupons.Web.Controllers;
using Moq;
using NUnit.Framework;
namespace KK.Store.DigitalCoupons.Web.Tests.Controllers
{
public class ContentResolverTests
{
[TestFixture]
public class when_resolving_the_content_images
{
[Test]
public void then_should_resolve_to_proper_location()
{
// Arrange
var resolver = new ContentResolver();
// Act
var httpContextBase = new Mock<HttpContextBase>();
var httpRequestBase = new Mock<HttpRequestBase>();
httpContextBase.Setup(@base => @base.Request).Returns(httpRequestBase.Object);
httpRequestBase.Setup(@base => @base.ApplicationPath).Returns("/Test");
var requestContext = new Mock<RequestContext>();
requestContext.SetupGet(context => context.HttpContext).Returns(httpContextBase.Object);
httpRequestBase.SetupGet(@base => @base.RequestContext).Returns(requestContext.Object);
var url = resolver.Resolve("~/Content/loading.gif", httpRequestBase.Object);
// Assert
Assert.That(url, Is.EqualTo("/Test/Content/loading.gif"));
}
}
}
}
using System.Web;
namespace KK.Store.DigitalCoupons.Web.Controllers
{
public interface IContentResolver
{
string Resolve(string imageLocation, HttpRequestBase httpRequestBase);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment