vendredi 26 juin 2015

Error when using Machine.Specs to test class using MVC MailerBase

As the title says I'm receiving an error when I try to test a class(FindTask) that is making a call to another class(NotificationMailer) that inherits from MVC MailerBase. The error I'm receiving is

System.ArgumentNullException: Value cannot be null.

Parameter name: httpContext

The relevant part of FindTask's body looks like:

class FindTask
{
     public void Find ()
     {
         notificationMailer.Notify ("Some message").Send ();
     }
}

I'm trying to verify that FindTask calls NotificationMailer's Notify.

The NotificationMailerBody looks like this:

public class NotificationMailer : MailerBase, INotificationMailer
    {
        public NotificationMailer()
        {
            MasterName = "_Layout";
        }

        public virtual MvcMailMessage Notify(string message)
        {
            return Populate(x =>
            {
                x.Subject = "Some Notification";
                x.ViewName = "Notify";
                x.To.Add("testEmail@test.com");
                x.Body = message;
            });
        }
    }

The error is on the return Populate(...) line.

The relevant part of my test looks like:

class FindTaskSpec : WithSubject<FindTask>
    {
        private Establish context = () =>
        {
            MvcMailMessage mailMessage = new MvcMailMessage ();
            The<INotificationMailer>().WhenToldTo(x => x.Notify(Param<string>.IsAnything))
                                      .Return(mailMessage); 
        };
        Because of = () => Subject.Find();
    }

    class when_something_is_found : FindTaskSpec
    {
        It should_send_an_email_with_the_found_stuff =
            () => The<IDeadLinkMailer> ().WasToldTo 
                  (x => x.Notify (Param<string>.IsAnything)).OnlyOnce ();
    }

I think the line in Establish context(the mock) should dictate that if Notify is called, it should return a new MVCMailMessage without running through the body of the function.

My Questions:

1.) How can I resolve this error and test to make sure the Notify method is called?

2.) Why is it that the mock for Notify does not stop the test from entering the function body of notify?

On a side note I've already tried setting MailerBase.IsTestModeEnabled = true. This results in an Url error - Invalid Url: The Url is Empty;

I've read through their wiki page on setting up tests for MvcMailer.

I've also read almost all the other stack overflow pages on this. I think that this link was most helpful. The problem is that I don't know how to imitate what they do in this link with Machine.Specifications.

I think my issue would be resolved if I could create a mock for either of the methods shown in the links.

Aucun commentaire:

Enregistrer un commentaire