1

I'm trying to create Unit Test for code that using ActionMailer, after I wrote the test I got System.ArgumentNullException error for httpContext parameter.

MailController.cs:

new Mailer().Welcome(new WelcomeModel
{
    Name = "sss",
    Email = model.Email
}).Deliver();

MailControllerTest.cs:

var model = new WelcomeInputModel{ Email = "dsadsa@gmail.com" };
var result = _controller.SendMail(model);
Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));

What should to do?

UPDATE, I updated my code:

MailControllerTest.cs:

var request = new Mock<HttpRequestBase>();
request.Setup(r => r.HttpMethod).Returns("GET");
_mockHttoContextBase.Setup(c => c.Request).Returns(request.Object);

MailController.cs:

var controller = new MailController {HttpContextBase = _contextBase};
controller.Forgot(new ForgotModel
{
    UserName = membershipUser.UserName, 
    Email = user.Email, 
    Password = membershipUser.ResetPassword()
}).Deliver();

Now the error that I getting is: System.NullReferenceException: Object reference not set to an instance of an object.

Zilberman Rafael
  • 1,341
  • 2
  • 14
  • 45

1 Answers1

0

You don't need to fake the httpcontext, just break the dependence between your controller and the ActionMailer. I wrote some wrappers to you.

Mailer.cs:

public interface IMailer
{
    void SendMail(string viewName, IEnumerable<string> to, string subject, IEnumerable<string> replayTo);

    void SendMail(string viewName, object model, IEnumerable<string> to, string subject,
        IEnumerable<string> replayTo);

    void SendMail(string viewName, IEnumerable<string> to, string from, string subject, IEnumerable<string> replayTo);

    void SendMail(string viewName, object model, IEnumerable<string> to, string from, string subject,
        IEnumerable<string> replayTo);
}

public class Mailer : MailerBase, IMailer
{
    public void SendMail(string viewName, IEnumerable<string> to, string subject,
        IEnumerable<string> replayTo = null)
    {
        foreach (var email in to)
        {
            To.Add(email);
        }
        Subject = subject;
        if (replayTo != null)
            foreach (var email in replayTo)
            {
                ReplyTo.Add(email);
            }

        Email(viewName).Deliver();
    }

    public void SendMail(string viewName, object model, IEnumerable<string> to, string subject,
        IEnumerable<string> replayTo = null)
    {
        foreach (var email in to)
        {
            To.Add(email);
        }
        Subject = subject;
        if (replayTo != null)
            foreach (var email in replayTo)
            {
                ReplyTo.Add(email);
            }

        Email(viewName, model).Deliver();
    }

    public void SendMail(string viewName, IEnumerable<string> to, string from, string subject,
        IEnumerable<string> replayTo)
    {
        foreach (var email in to)
        {
            To.Add(email);
        }
        From = from;
        Subject = subject;
        if (replayTo != null)
            foreach (var email in replayTo)
            {
                ReplyTo.Add(email);
            }

        Email(viewName).Deliver();
    }

    public void SendMail(string viewName, object model, IEnumerable<string> to, string from, string subject,
        IEnumerable<string> replayTo)
    {
        foreach (var email in to)
        {
            To.Add(email);
        }
        From = from;
        Subject = subject;
        if (replayTo != null)
            foreach (var email in replayTo)
            {
                ReplyTo.Add(email);
            }

        Email(viewName, model).Deliver();
    }
}

To use the wrapper, bind him using Ninject or whatever library you want and use it in your controllers.

NinjectWebCommon.cs:

_kernel.Bind<IMailer>().To<Mailer>();

MailController.cs:

private readonly IMailer _mailer;

public MailController(IMailer mailer) {
    _mailer = mailer;
}

_mailer.SendMail("Forgot", new ForgotModel
{
    UserName = membershipUser.UserName, 
    Email = user.Email, 
    Password = membershipUser.ResetPassword()
}, new List<string> { model.Email }, _myEmail, "Your password", new List<string>());
Zilberman Rafael
  • 1,341
  • 2
  • 14
  • 45