1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
.TestSupport
18 using System
.Collections
.Generic
;
20 using Castle
.MonoRail
.Framework
;
21 using Castle
.MonoRail
.Framework
.Test
;
24 /// Base class to test view components.
28 /// The following test makes sure the component rendered the inner sections correctly.
31 /// public class DiggStylePaginationTestCase : BaseViewComponentTest
33 /// private DiggStylePagination diggComponent;
34 /// private IPaginatedPage singlePage, secondPageOfThree;
37 /// public void Init()
39 /// diggComponent = new DiggStylePagination();
40 /// singlePage = new Page(new string[] { "a", "b", "c" }, 1, 4, 1);
41 /// secondPageOfThree = new Page(new string[] { "a", "b", "c", "d" }, 2, 4, 10);
45 /// public void Terminate()
51 /// public void PageWithNoLinksInvokesStartAndEndSections()
53 /// List<string> actions = new List<string>();
54 /// // pass mock inner sections to component
55 /// SectionRender["startblock"] = delegate(IDictionary context, TextWriter writer) { actions.Add("started"); };
56 /// SectionRender["endblock"] = delegate(IDictionary context, TextWriter writer) { actions.Add("ended"); };
57 /// SectionRender["link"] = delegate(IDictionary context, TextWriter writer) { actions.Add("link"); };
59 /// diggComponent.Page = singlePage;
61 /// PrepareViewComponent(diggComponent);
62 /// diggComponent.Render();
64 /// // make sure component "rendered" inner sections
65 /// Assert.AreEqual(2, actions.Count);
66 /// Assert.AreEqual("started", actions[0]);
67 /// Assert.AreEqual("ended", actions[1]);
74 /// You must call <see cref="PrepareViewComponent"/> before testing a view component instance
75 /// and you should call <see cref="CleanUp"/> after each test case (use the TearDown).
77 public abstract class BaseViewComponentTest
: BaseControllerTest
79 private IMockViewComponentContext componentContext
;
80 private IViewEngine viewEngine
;
81 private StringWriter writer
;
84 /// Use this dictionary to add inner sections as available inner sections to
85 /// the view component.
87 public IDictionary
<string, TestSectionRender
> SectionRender
;
90 /// This delegate is called when the viewcomponent renders its body.
92 public TestSectionRender OnBodyRender
;
95 /// This delegate is called when the viewcomponent renders a view
97 public TestViewRender OnViewRender
;
100 /// Initializes a new instance of the <see cref="BaseViewComponentTest"/> class.
102 protected BaseViewComponentTest()
108 /// Initialize the view component with mock services it needs to
111 /// <param name="component">The component instance.</param>
112 protected void PrepareViewComponent(ViewComponent component
)
116 BuildEngineContext("", "Controller", "Action");
119 viewEngine
= BuildViewEngine();
121 componentContext
= BuildViewComponentContext(component
.GetType().Name
);
123 component
.Init(Context
, componentContext
);
127 /// Gets the output -- ie what the viewcomponent wrote to the output stream.
129 /// <value>The output.</value>
130 protected string Output
132 get { return writer.ToString(); }
136 /// Cleans the up all state created to test a view component.
138 protected void CleanUp()
140 writer
= new StringWriter();
141 SectionRender
= new Dictionary
<string, TestSectionRender
>(StringComparer
.InvariantCultureIgnoreCase
);
147 /// Builds the view engine.
149 /// <returns></returns>
150 protected virtual IViewEngine
BuildViewEngine()
152 return new ViewEngineStub(".view", ".jsview", true);
156 /// Builds the view component context.
158 /// <param name="viewComponentName">Name of the view component.</param>
159 /// <returns></returns>
160 protected virtual IMockViewComponentContext
BuildViewComponentContext(string viewComponentName
)
162 MockViewComponentContext compContext
= new MockViewComponentContext(viewComponentName
, writer
, viewEngine
);
164 compContext
.SectionRender
= SectionRender
;
165 compContext
.OnBodyRender
= OnBodyRender
;
166 compContext
.OnViewRender
= OnViewRender
;