More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Services / EmailTemplateServiceTestCase.cs
blobd8dc3c722f3429e1b54ad15c16b7a8dc72fb4588
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Tests.Services
17 using System.Collections;
18 using System.Collections.Generic;
19 using System.IO;
20 using Castle.Components.Common.EmailSender;
21 using NUnit.Framework;
22 using Rhino.Mocks;
23 using Rhino.Mocks.Constraints;
24 using Test;
26 [TestFixture]
27 public class EmailTemplateServiceTestCase
29 private MockRepository mockRepository = new MockRepository();
30 private EmailTemplateService service;
31 private IViewEngineManager viewEngineManagerMock;
32 private MockEngineContext engineContext;
33 private DummyController controller;
34 private ControllerContext controllerContext;
36 [SetUp]
37 public void Init()
39 viewEngineManagerMock = mockRepository.CreateMock<IViewEngineManager>();
41 service = new EmailTemplateService(viewEngineManagerMock);
43 engineContext = new MockEngineContext(null, null, null, null);
44 controller = new DummyController();
45 controllerContext = new ControllerContext();
48 [Test]
49 public void RenderMailMessage_BackwardCompatibility_PassOnControllerContext()
51 string templateName = "welcome";
53 using(mockRepository.Record())
55 viewEngineManagerMock.Process(templateName, null, engineContext, controller, controllerContext);
56 LastCall.Constraints(
57 Is.Equal("mail\\" + templateName),
58 Is.Anything(),
59 Is.Same(engineContext),
60 Is.Same(controller),
61 Is.Same(controllerContext));
64 using(mockRepository.Playback())
66 service.RenderMailMessage(templateName, engineContext, controller, controllerContext, false);
70 [Test]
71 public void RenderMailMessage_BackwardCompatibility_UsesTemplateNameAsItIsIfStartsWithSlash()
73 string templateName = "/emailtemplates/welcome";
75 using(mockRepository.Record())
77 viewEngineManagerMock.Process(templateName, null, engineContext, controller, controllerContext);
78 LastCall.Constraints(
79 Is.Equal(templateName),
80 Is.Anything(),
81 Is.Same(engineContext),
82 Is.Same(controller),
83 Is.Same(controllerContext));
86 using(mockRepository.Playback())
88 service.RenderMailMessage(templateName, engineContext, controller, controllerContext, false);
92 [Test]
93 public void RenderMailMessage_InvokesViewEngineManager()
95 string templateName = "welcome";
96 Hashtable parameters = new Hashtable();
98 using(mockRepository.Record())
100 viewEngineManagerMock.Process(templateName, "layout", null, null);
101 LastCall.Constraints(
102 Is.Equal("mail\\" + templateName),
103 Is.Equal("layout"),
104 Is.Anything(),
105 Is.Anything());
108 using(mockRepository.Playback())
110 service.RenderMailMessage(templateName, "layout", parameters);
114 [Test]
115 public void RenderMailMessage_MessageIsConstructedCorrectly()
117 string templateName = "welcome";
118 Hashtable parameters = new Hashtable();
120 using(mockRepository.Record())
122 Expect.Call(delegate() { viewEngineManagerMock.Process(templateName, "layout", null, null); })
123 .Constraints(
124 Is.Equal("mail\\" + templateName),
125 Is.Equal("layout"),
126 Is.Anything(),
127 Is.Anything())
128 .Do(new Render(RendersEmail));
131 using(mockRepository.Playback())
133 Message message = service.RenderMailMessage(templateName, "layout", parameters);
135 Assert.AreEqual("hammett@noemail.com", message.To);
136 Assert.AreEqual("copied@noemail.com", message.Cc);
137 Assert.AreEqual("bcopied@noemail.com", message.Bcc);
138 Assert.AreEqual("contact@noemail.com", message.From);
139 Assert.AreEqual("Hello!", message.Subject);
140 Assert.AreEqual("This is the\r\nbody\r\n", message.Body);
141 Assert.AreEqual(1, message.Headers.Count);
145 public delegate void Render(
146 string templateName, string layoutName, TextWriter output, IDictionary<string, object> parameters);
148 public static void RendersEmail(string templateName, string layoutName, TextWriter output,
149 IDictionary<string, object> parameters)
151 output.WriteLine("to: hammett@noemail.com");
152 output.WriteLine("cc: copied@noemail.com");
153 output.WriteLine("bcc: bcopied@noemail.com");
154 output.WriteLine("from: contact@noemail.com");
155 output.WriteLine("subject: Hello!");
156 output.WriteLine("X-something: Mime-super-content");
157 output.WriteLine("");
158 output.WriteLine("This is the");
159 output.WriteLine("body");
162 private class DummyController : Controller