2

In a new project we use MvcMailer, it's great and I would like to know how I can test it using Rhino and NUnit? There's an other post on SO and a good Wiki page but it's not what I'm looking for. For my controllers I usualy test them with MvcContrib's Testhelper

  • I try first to mock the mailer class but if I do this I can't verify my ViewBag data, I'm having problem with the PopulateBody method and I have to build my own IMailerBase interface
  • I try after this to test the mailer as I do with controller using MvcContrib but it only accept Controller object in the InitializeController() so it did'nt work.
  • There's also a IsTestModeEnabled property in the MailerBase.cs class but when I test against it I get an error on empty URI.

Don't know what is the best way to do this and I'm looking for help, thanks everyone!

Community
  • 1
  • 1
VinnyG
  • 6,883
  • 7
  • 58
  • 76

1 Answers1

4

Here's the code how I did it :

// arrange
    var passwordMailer = MockRepository.GeneratePartialMock<PasswordMailer>();
    passwordMailer.Stub(mailer => mailer.PopulateBody(Arg<MailMessage>.Is.Anything, Arg.Is("ForgotPassword"), Arg<string>.Is.Null, Arg<Dictionary<string, string>>.Is.Null));

    // act
    var mailMessage = passwordMailer.ForgotPassword("test@example.com", "4454-8838-73773");

    // assert
    Assert.AreEqual(string.Format(Login.mailBody, "4454-8838-73773"), passwordMailer.ViewBag.Body);
    Assert.AreEqual(Login.mailSubject, mailMessage.Subject);
    Assert.AreEqual("test@example.com", mailMessage.To.First().ToString());

As you can see it, you can achieve it using partialMock functionnality from Rhino.

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
  • Thanks Thomas, can you tell me how GeneratePartialMock id different from a DynamicMock? – VinnyG Jun 14 '11 at 14:38
  • 1
    A normal mock object will throw an exception when a method, which has no explicit expectation defined, is called on the mock instance. A dynamic mock, instead of throwing an exception, will return null or 0 for any unexpected method calls. A partial mock, like a dynamic mock, will not throw an exception if a method is called when there is no expectation defined for the method; but, instead of returning 0 or null, a partial mock will call the actual implementation method (i.e., not the mock) on the object and return that value. This lets you selectively mock specific methods on an object. – Tomasz Jaskuλa Jun 14 '11 at 19:18
  • that's why I mock only PopulateBody method and let the default implementation do the rest – Tomasz Jaskuλa Jun 14 '11 at 19:21