using System.Web; using System.Web.Routing; using Rhino.Mocks; using Xunit; using System.Web.Mvc; namespace Website.Tests { public class HtmlExtensionTests { private HtmlHelper CreateHtmlHelper(string appPath) { HttpContextBase httpContext = MockRepository.GenerateStub(); HttpRequestBase httpRequestBase = MockRepository.GenerateStub(); httpRequestBase.Stub(h => h.ApplicationPath).Return(appPath); httpContext.Stub(h => h.Request).Return(httpRequestBase); ViewContext viewContext = MockRepository.GenerateStub(httpContext, new RouteData(), MockRepository.GenerateStub(), "Test", "MasterTest", new ViewDataDictionary(), new TempDataDictionary(httpContext)); return new HtmlHelper(viewContext, MockRepository.GenerateStub()); } [Fact] public void Resolve_With_IIS_Virtual_Directory_Returns_Full_Path() { HtmlHelper html = CreateHtmlHelper("http://localhost/TestProject"); string result = html.Resolve("~/Content/t.jpg"); Assert.Equal("http://localhost/TestProject/Content/t.jpg", result); } [Fact] public void Resolve_With_IIS_Virtual_Directory_Ends_With_Slash_Returns_Full_Path() { string appPath = "http://localhost/TestProject/"; HtmlHelper html = CreateHtmlHelper(appPath); string result = html.Resolve("~/Content/t.jpg"); Assert.Equal(appPath + "Content/t.jpg", result); } [Fact] public void Resolve_With_IIS_Root_Returns_Full_Path() { string appPath = "http://localhost"; HtmlHelper html = CreateHtmlHelper(appPath); string result = html.Resolve("~/Content/t.jpg"); Assert.Equal(appPath + "/Content/t.jpg", result); } [Fact] public void Resolve_Without_StrangeLine_And_Slash_Returns_String_AsWas() { string appPath = "http://localhost"; HtmlHelper html = CreateHtmlHelper(appPath); string result = html.Resolve("Content/t.jpg"); Assert.Equal("Content/t.jpg", result); } [Fact] public void Resolve_With_App_Path_Empty_Should_Still_Return_String() { HtmlHelper html = CreateHtmlHelper(string.Empty); string result = html.Resolve("~/Content/t.jpg"); Assert.Equal("/Content/t.jpg", result); } [Fact] public void Resolve_Two_Sub_Directories_Returns_Both_Directories() { string appPath = "http://localhost/Test/Views"; HtmlHelper html = CreateHtmlHelper(appPath); string result = html.Resolve("~/Content/t.jpg"); Assert.Equal(appPath + "/Content/t.jpg", result); } } }