MvcMailer unit tests: System.ArgumentNullException httpContext cannot be null

I cannot successfully run unit tests for MvcMailer using the visual studio test suite and Moq. I have copied the example from the wiki word for word but get the following exception every time:

Test method MvcMailerTest.Tests.MailerTest.TestMethod1 threw exception: 
System.ArgumentNullException: Value cannot be null.
Parameter name: httpContext

Code is as follows: (Using the VS unit test framework - exact same error when using nUnit as in the example)

        //Arrange: Moq out the PopulateBody method
        var _userMailerMock = new Mock<UserMailer>();
        _userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));            
        _userMailerMock.CallBase = true;

        var mailMessage = _userMailerMock.Object.Welcome();

Fails on the following line in the Welcome() method (copied straight out of the wiki):

 PopulateBody(mailMessage, viewName: "Welcome");

The wiki is here:

Similar (almost exactly the same) Question: MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action

Anyone know how to fix/get around this? The linked question says I need to mock out the PopulateBody method which I have done (as per wiki).


a workaround is to change your code to this:

PopulateBody(mailMessage, "Welcome", null);

This will work because you have a mock setup for that overload of PopulateBody and not for the 2 parameter version of it..

A quick addition to Filip's answer that somebody might find helpful: I am using version 4.0 of the MvcMailer package. I was using the Populate(Action<MvcMailMessage> action) method inside my mailer actions and noticed that it uses a four-parameter version of PopulateBody:

// Mvc.Mailer.MailerBase (using ILSpy)
public virtual MvcMailMessage Populate(Action<MvcMailMessage> action)
    MvcMailMessage mvcMailMessage = new MvcMailMessage();

    // Four parameters! (comment added by me)
    this.PopulateBody(mvcMailMessage, mvcMailMessage.ViewName, mvcMailMessage.MasterName, mvcMailMessage.LinkedResources);

    return mvcMailMessage;

As such, I found that setting up the mock with four parameters...

PopulateBody(mailMessage, "Welcome", "SomeMasterName", null);

...did the trick.

Need Your Help

How do I compare 2 different fields in Magento in a collection

php magento

I am attempting to compare products price to its sale price in a Magento collection. Specifically, trying to find all the products who have a sale price that is greater than or equal to its base pr...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.