1

我正在尝试为使用 ActionMailer 的代码创建单元测试,在我编写测试后,我收到 httpContext 参数的 System.ArgumentNullException 错误。

邮件控制器.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));

应该怎么做?

更新,我更新了我的代码:

MailControllerTest.cs

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

邮件控制器.cs

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

现在我得到的错误是: System.NullReferenceException: Object reference not set to an instance of an object。

4

1 回答 1

0

您不需要伪造 httpcontext,只需打破控制器与ActionMailer. 我给你写了一些包装。

邮件程序.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();
    }
}

要使用包装器,请使用 Ninject 或您想要的任何库绑定他并在控制器中使用它。

NinjectWebCommon.cs

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

邮件控制器.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>());
于 2014-01-01T15:18:07.023 回答