andrewdavey/postal

Unit Test & Send mails with Postal

Opened this issue · 3 comments

Hey Guys,

First of all love the product, been using it for years now!

Recently I started a Visual Studio WebApi tests project because our usage of Postal and emails start to grow. The goal of this project is to test all of our mails (inclusive sending them to our inbox for review)

// Arrange
var controller = new MailApiController(new EmailService());
// Act
var result = controller.MailOnSignup();
// Assert
Assert.IsNotNull(result);

The problem however is that I receive the following error when the send method of the mail is triggered within the actual controller.

Value cannot be null; httpBrowserCapabilities
StackTrace " bij System.Web.HttpBrowserCapabilitiesWrapper..ctor(HttpBrowserCapabilities httpBrowserCapabilities)\r\n bij System.Web.HttpRequestWrapper.get_Browser()\r\n bij System.Web.WebPages.BrowserHelpers.GetOverriddenBrowser(HttpContextBase httpContext, Func2 createBrowser)\r\n bij System.Web.WebPages.DisplayModeProvider.<.ctor>b__2(HttpContextBase context)\r\n bij System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext)\r\n bij System.Web.WebPages.DisplayModeProvider.<GetAvailableDisplayModesForContext>d__4.MoveNext()\r\n bij System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)\r\n bij System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)\r\n bij System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass6.<FindView>b__4(IViewEngine e)\r\n bij System.Web.Mvc.ViewEngineCollection.Find(Func2 lookup, Boolean trackSearchedPaths)\r\n bij System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName)\r\n bij Postal.EmailViewRenderer.CreateView(String viewName, ControllerContext controllerContext)\r\n bij Postal.EmailViewRenderer.Render(Email email, String viewName)\r\n bij Postal.EmailService.Send(Email email)\r\n bij CallSite.Target(Closure , CallSite , IEmailService , Object )\r\n bij System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)\r\n

Any idea what I'm missing or what the issue could be?

Thanks in advance!

Running into the same issue. Anyone know of a solution or workaround?

It may be the HttpContext is null and MVC viewengine cannot be initiated out of the http pipeline. Please try mock the HttpContext class and assign tonHttpContext.Current

Very late to the party here but http://aboutcode.net/postal/unit-testing.html should help you to get this working in unit tests.