RECAP
I’ve been pretty busy as of late and even had some issues with the way I was trying to unit test validation. As you can see here, I was able to get some help from Joey on this and it has made me realize that I need to re-write my MonoRail Validation post to use the “correct” manner of validation. Even Oren agrees that the validation library in MonoRail works, it is just not the ideal way of doing it.
SENDING AN EMAIL
You have to do 3 things to send an email successfully:
1. Write your unit test showing your expectations of the code:
1: using Castle.MonoRail.TestSupport;
2: using JasonMeridth.Controllers;
3: using JasonMeridth.Models;
4: using NUnit.Framework;
5:
6: namespace JasonMeridth.Tests.Controllers
7: {
8: [TestFixture]
9: public class ContactControllerTestCase : BaseControllerTest
10: {
11: private Contact contact;
12: private ContactController contactController;
13:
14: [SetUp]
15: public void Setup_context()
16: {
17: contactController = new ContactController();
18: PrepareController(contactController, "", "Contact", "SendContactMessage");
19: contact = CreateDummyContact();
20: }
21: ....
22: [Test]
23: public void Should_send_contact_an_email_and_render_confirmation_view_when_contact_is_valid()
24: {
25: contactController.SendContactMessage(contact);
26:
27: Assert.AreEqual(@"Contactconfirmation", contactController.SelectedViewName);
28:
29: Assert.IsNotNull(contactController.PropertyBag["contact"]);
30: Assert.AreEqual(1, MessagesSent.Length);
31: }
32: ....
33: private Contact CreateDummyContact()
34: {
35: Contact dummyContact = new Contact();
36: dummyContact.Name = "Dummy";
37: dummyContact.Message = "I'm a dummy";
38: dummyContact.Email = "dummy@dummy.com";
39:
40: return dummyContact;
41: }
42: }
43: }
2. Set your SMTP server in the web.config file:
1: <monorail smtpHost="<yourSmtpServer>.com" useWindsorIntegration="false">
2: <controllers>
3: <assembly>JasonMeridth</assembly>
4: </controllers>
5: <viewEngine viewPathRoot="Views" customEngine="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
6: </monorail>
3. Make your test pass by adding the line to your controller’s action (in my case the ContactController):
1: public void SendContactMessage([DataBind("contact", Validate=true)] Contact contact)
2: {
3: if (HasValidationError(contact))
4: {
5: Flash["contact"] = contact;
6: Flash["summary"] = GetErrorSummary(contact);
7: RedirectToAction("contactform");
8: }
9:
10: Message emailMessage = new Message(contact.Email, "yourEmail@email.com", "Contact from YourWebsite.com website", contact.Message);
11: DeliverEmail(emailMessage);
12:
13: PropertyBag["contact"] = contact;
14: RenderView("confirmation");
15: }
DeliverEmail is build-into the MonoRail.Controller class. Use it. :)
AUTHENTICATION & AUTHORIZATION
NEXT POST: Hooking up NHibernate
Prior Posts:
Monorail #0:Controllers
Monorail #1:Reasons, Setup, and First Output
Monorail #2:Layouts and Rescues
Monorail #3:Unit Testing and ViewComponents
Monorail #4: Validation
Post Footer automatically generated by Add Post Footer Plugin for wordpress.
