Commits

Scott Anderson committed 34387a9

Fixing area detection per #42. Should be working now.

Comments (0)

Files changed (4)

src/ActionMailer.Net.Mvc/MailerBase.cs

         }
 
         private string FindAreaName() {
-            var name = GetType().Namespace;
-            if (name != null && name.Contains(".Areas.")) {
-                var startIndex = name.IndexOf(".Areas.") + 7;
-                var length = name.LastIndexOf(".") - startIndex;
-                return name.Substring(startIndex, length);
+            string area = null;
+
+            if (this.HttpContextBase != null &&
+                this.HttpContextBase.Request != null &&
+                this.HttpContextBase.Request.RequestContext != null &&
+                this.HttpContextBase.Request.RequestContext.RouteData != null) {
+
+                    if (this.HttpContextBase.Request.RequestContext.RouteData.DataTokens.ContainsKey("area")) {
+                        area = this.HttpContextBase.Request.RequestContext.RouteData.DataTokens["area"].ToString();
+                    }
             }
 
-            return null;
+            if (area == null) {
+                var name = GetType().Namespace;
+                if (name != null && name.Contains(".Areas.")) {
+                    var startIndex = name.IndexOf(".Areas.") + 7;
+                    var length = name.LastIndexOf(".") - startIndex;
+                    area = name.Substring(startIndex, length);
+                }
+            }
+
+            return area;
         }
 
         /// <summary>

src/ActionMailer.Net.Tests/Mvc/MailerBaseTests.cs

 using System.Web.Mvc;
 using FakeItEasy;
 using Xunit;
+using System.Web.Routing;
 
 namespace ActionMailer.Net.Tests.Mvc {
     public class MailerBaseTests {
             var mockSender = A.Fake<IMailSender>();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-
+            
             var mailer = new TestMailerBase(mockSender);
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
             var result = mailer.Email("TestView");
 
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             mailer.ViewBag.Test = "12345";
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             object model = "12345";
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             mailer.ViewData["foo"] = "bar";
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             // there's no need to test the built-in view engines.
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new UTF8ViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
             mailer.MessageEncoding = Encoding.UTF8;
 
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new MultipartViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             // there's no need to test the built-in view engines.
             var mailer = new TestMailController();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
 
             var email = mailer.TestMail();
 
             var mailer = new TestMailController();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
 
             var email = mailer.TestMaster();
 
         [Fact]
         public void ViewNameShouldBeRequiredWhenUsingCallingEmailMethod() {
             var mailer = new TestMailerBase();
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
 
             Assert.Throws<ArgumentNullException>(() => {
                 mailer.Email(null);
 
         [Fact]
         public void AreasAreDetectedProperly() {
+            var rd = new RouteData();
+            rd.Values.Add("area", "TestArea");
             var mailer = new Areas.TestArea.Controllers.MailController();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new TextViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
 
             mailer.TestEmail();
 
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new WhiteSpaceViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             // there's no need to test the built-in view engines.
             var mailer = new TestMailerBase();
             ViewEngines.Engines.Clear();
             ViewEngines.Engines.Add(new WhiteSpaceViewEngine());
-            mailer.HttpContextBase = new EmptyHttpContextBase();
+            mailer.HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null);
             mailer.From = "no-reply@mysite.com";
 
             // there's no need to test the built-in view engines.

src/ActionMailer.Net.Tests/Mvc/Mocks.cs

 using ActionMailer.Net.Tests.Mvc;
 
 namespace ActionMailer.Net.Tests.Mvc {
-    public class EmptyHttpContextBase : HttpContextBase { }
-
     public class TestMailerBase : MailerBase {
         public TestMailerBase(IMailSender sender = null, Encoding defaultMessageEncoding = null)
             : base(sender, defaultMessageEncoding) { }
 
     public class TestController : Controller {
         public string TestAction() {
-            var email = new TestMailController { HttpContextBase = new EmptyHttpContextBase() }.TestMail();
+            var email = new TestMailController { HttpContextBase = MvcHelper.GetHttpContext("/app/", null, null) }.TestMail();
             return email.ViewName;
         }
     }

src/ActionMailer.Net.Tests/Mvc/MvcHelper.cs

 using System.Collections;
 using System.Web;
 using FakeItEasy;
+using System.Web.Routing;
 
 namespace ActionMailer.Net.Tests.Mvc {
     // Some helpers yanked from the MVC 3 source.
     public static class MvcHelper {
         public const string AppPathModifier = "/$(SESSION)";
 
-        public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod, string protocol, int port) {
+        public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod, string protocol, int port, RouteData routeData) {
             var httpContext = A.Fake<HttpContextBase>();
+            var requestContext = A.Fake<RequestContext>();
 
             // required because FakeItEasy will initialize this, and then MVC will use
             // it to determine if the Url was rewritten (which returns true, which
             else
                 uri = new Uri(protocol + "://localhost");
 
+            A.CallTo(() => requestContext.RouteData).Returns(routeData);
+            
             A.CallTo(() => httpContext.Request.Url).Returns(uri);
+            A.CallTo(() => httpContext.Request.RequestContext).Returns(requestContext);
+            A.CallTo(() => httpContext.Request.PathInfo).Returns(string.Empty);
 
-            A.CallTo(() => httpContext.Request.PathInfo).Returns(string.Empty);
             if (!String.IsNullOrEmpty(httpMethod)) {
                 A.CallTo(() => httpContext.Request.HttpMethod).Returns(httpMethod);
             }
         }
 
         public static HttpContextBase GetHttpContext(string appPath, string requestPath, string httpMethod) {
-            return GetHttpContext(appPath, requestPath, httpMethod, Uri.UriSchemeHttp, -1);
+            return GetHttpContext(appPath, requestPath, httpMethod, Uri.UriSchemeHttp, -1, null);
         }
     }
 }