PageRenderTime 81ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/System.Web.Mvc.Test/Test/RazorViewTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 248 lines | 191 code | 33 blank | 24 comment | 0 complexity | 3f2b50fe25598c010002b878b37f0e05 MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web.WebPages;
  6. using Moq;
  7. using Xunit;
  8. using Assert = Microsoft.TestCommon.AssertEx;
  9. namespace System.Web.Mvc.Test
  10. {
  11. public class RazorViewTest
  12. {
  13. [Fact]
  14. public void Constructor_RunViewStartPagesParam()
  15. {
  16. var context = new ControllerContext();
  17. Assert.True(new RazorView(context, "~/view", "~/master", runViewStartPages: true, viewStartFileExtensions: null).RunViewStartPages);
  18. Assert.False(new RazorView(context, "~/view", "~/master", runViewStartPages: false, viewStartFileExtensions: null).RunViewStartPages);
  19. Assert.True(new RazorView(context, "~/view", "~/master", runViewStartPages: true, viewStartFileExtensions: null, viewPageActivator: new Mock<IViewPageActivator>().Object).RunViewStartPages);
  20. Assert.False(new RazorView(context, "~/view", "~/master", runViewStartPages: false, viewStartFileExtensions: null, viewPageActivator: new Mock<IViewPageActivator>().Object).RunViewStartPages);
  21. }
  22. [Fact]
  23. public void ConstructorWithEmptyViewPathThrows()
  24. {
  25. // Act & Assert
  26. Assert.ThrowsArgumentNullOrEmpty(
  27. () => new RazorView(new ControllerContext(), String.Empty, "~/master", false, Enumerable.Empty<string>()),
  28. "viewPath"
  29. );
  30. }
  31. [Fact]
  32. public void ConstructorWithNullViewPathThrows()
  33. {
  34. // Act & Assert
  35. Assert.ThrowsArgumentNullOrEmpty(
  36. () => new RazorView(new ControllerContext(), null, "~/master", false, Enumerable.Empty<string>()),
  37. "viewPath"
  38. );
  39. }
  40. [Fact]
  41. public void ConstructorWithNullControllerContextThrows()
  42. {
  43. // Act & Assert
  44. Assert.ThrowsArgumentNull(
  45. () => new RazorView(null, "view path", "~/master", false, Enumerable.Empty<string>()),
  46. "controllerContext"
  47. );
  48. }
  49. [Fact]
  50. public void LayoutPathProperty()
  51. {
  52. //Arrange
  53. ControllerContext controllerContext = new ControllerContext();
  54. // Act
  55. RazorView view = new RazorView(new ControllerContext(), "view path", "master path", false, Enumerable.Empty<string>());
  56. // Assert
  57. Assert.Equal("master path", view.LayoutPath);
  58. }
  59. [Fact]
  60. public void LayoutPathPropertyReturnsEmptyStringIfNullLayoutSpecified()
  61. {
  62. // Act
  63. RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty<string>());
  64. // Assert
  65. Assert.Equal(String.Empty, view.LayoutPath);
  66. }
  67. [Fact]
  68. public void LayoutPathPropertyReturnsEmptyStringIfLayoutNotSpecified()
  69. {
  70. // Act
  71. RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty<string>());
  72. // Assert
  73. Assert.Equal(String.Empty, view.LayoutPath);
  74. }
  75. [Fact]
  76. public void RenderWithNullWriterThrows()
  77. {
  78. // Arrange
  79. RazorView view = new RazorView(new ControllerContext(), "~/viewPath", null, false, Enumerable.Empty<string>());
  80. Mock<ViewContext> viewContextMock = new Mock<ViewContext>();
  81. MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
  82. view.BuildManager = buildManager;
  83. // Act & Assert
  84. Assert.ThrowsArgumentNull(
  85. () => view.Render(viewContextMock.Object, null),
  86. "writer"
  87. );
  88. }
  89. [Fact]
  90. public void RenderWithUnsupportedTypeThrows()
  91. {
  92. // Arrange
  93. ViewContext context = new Mock<ViewContext>().Object;
  94. MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(object));
  95. RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty<string>());
  96. view.BuildManager = buildManagerMock;
  97. // Act & Assert
  98. Assert.Throws<InvalidOperationException>(
  99. () => view.Render(context, new Mock<TextWriter>().Object),
  100. "The view at 'view path' must derive from WebViewPage, or WebViewPage<TModel>."
  101. );
  102. }
  103. [Fact]
  104. public void RenderWithViewPageAndNoStartPageLookupRendersView()
  105. {
  106. // Arrange
  107. StubWebViewPage viewPage = new StubWebViewPage();
  108. Mock<ViewContext> viewContextMock = new Mock<ViewContext>();
  109. viewContextMock.Setup(vc => vc.HttpContext.Items).Returns(new Dictionary<object, object>());
  110. viewContextMock.Setup(vc => vc.HttpContext.Request.IsLocal).Returns(false);
  111. MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
  112. Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict);
  113. ControllerContext controllerContext = new ControllerContext();
  114. activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage);
  115. RazorView view = new RazorView(controllerContext, "~/viewPath", null, false, Enumerable.Empty<string>(), activator.Object);
  116. view.StartPageLookup = (WebPageRenderingBase p, string n, IEnumerable<string> e) =>
  117. {
  118. Assert.True(false, "ViewStart page lookup should not be called");
  119. return null;
  120. };
  121. view.BuildManager = buildManager;
  122. // Act
  123. view.Render(viewContextMock.Object, new Mock<TextWriter>().Object);
  124. // Assert
  125. Assert.Null(viewPage.Layout);
  126. Assert.Equal("", viewPage.OverridenLayoutPath);
  127. Assert.Same(viewContextMock.Object, viewPage.ViewContext);
  128. Assert.Equal("~/viewPath", viewPage.VirtualPath);
  129. }
  130. [Fact]
  131. public void RenderWithViewPageAndStartPageLookupExecutesStartPage()
  132. {
  133. // Arrange
  134. StubWebViewPage viewPage = new StubWebViewPage();
  135. Mock<ViewContext> viewContextMock = new Mock<ViewContext>();
  136. viewContextMock.Setup(vc => vc.HttpContext.Items).Returns(new Dictionary<object, object>());
  137. MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
  138. Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict);
  139. ControllerContext controllerContext = new ControllerContext();
  140. activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage);
  141. RazorView view = new RazorView(controllerContext, "~/viewPath", null, true, new[] { "cshtml" }, activator.Object);
  142. Mock<ViewStartPage> startPage = new Mock<ViewStartPage>();
  143. startPage.Setup(sp => sp.ExecutePageHierarchy()).Verifiable();
  144. view.StartPageLookup = (WebPageRenderingBase page, string fileName, IEnumerable<string> extensions) =>
  145. {
  146. Assert.Equal(viewPage, page);
  147. Assert.Equal("_ViewStart", fileName);
  148. Assert.Equal(new[] { "cshtml" }, extensions.ToArray());
  149. return startPage.Object;
  150. };
  151. view.BuildManager = buildManager;
  152. // Act
  153. view.Render(viewContextMock.Object, new Mock<TextWriter>().Object);
  154. // Assert
  155. startPage.Verify(sp => sp.ExecutePageHierarchy(), Times.Once());
  156. }
  157. // TODO: This throws in WebPages and needs to be tracked down.
  158. [Fact]
  159. public void RenderWithViewPageAndLayoutPageRendersView()
  160. {
  161. // Arrange
  162. StubWebViewPage viewPage = new StubWebViewPage();
  163. Mock<ViewContext> viewContext = new Mock<ViewContext>();
  164. Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>();
  165. Mock<HttpRequestBase> httpRequest = new Mock<HttpRequestBase>();
  166. httpRequest.SetupGet(r => r.IsLocal).Returns(false);
  167. httpRequest.SetupGet(r => r.Browser.IsMobileDevice).Returns(false);
  168. httpRequest.SetupGet(r => r.Cookies).Returns(new HttpCookieCollection());
  169. httpContext.SetupGet(c => c.Request).Returns(httpRequest.Object);
  170. httpContext.SetupGet(c => c.Response.Cookies).Returns(new HttpCookieCollection());
  171. httpContext.SetupGet(c => c.Items).Returns(new Hashtable());
  172. viewContext.SetupGet(v => v.HttpContext).Returns(httpContext.Object);
  173. MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
  174. Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict);
  175. Mock<WebPage> layoutPage = new Mock<WebPage> { CallBase = true };
  176. layoutPage.Setup(c => c.Execute()).Callback(() => layoutPage.Object.RenderBody());
  177. Mock<IVirtualPathFactory> virtualPathFactory = new Mock<IVirtualPathFactory>(MockBehavior.Strict);
  178. virtualPathFactory.Setup(f => f.Exists("~/layoutPath")).Returns(true);
  179. virtualPathFactory.Setup(f => f.CreateInstance("~/layoutPath")).Returns(layoutPage.Object);
  180. ControllerContext controllerContext = new ControllerContext();
  181. activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage);
  182. RazorView view = new RazorView(controllerContext, "~/viewPath", "~/layoutPath", false, Enumerable.Empty<string>(), activator.Object);
  183. view.BuildManager = buildManager;
  184. view.VirtualPathFactory = virtualPathFactory.Object;
  185. view.DisplayModeProvider = DisplayModeProvider.Instance;
  186. // Act
  187. view.Render(viewContext.Object, TextWriter.Null);
  188. // Assert
  189. Assert.Equal("~/layoutPath", viewPage.Layout);
  190. Assert.Equal("~/layoutPath", viewPage.OverridenLayoutPath);
  191. Assert.Same(viewContext.Object, viewPage.ViewContext);
  192. Assert.Equal("~/viewPath", viewPage.VirtualPath);
  193. }
  194. public class StubWebViewPage : WebViewPage
  195. {
  196. public bool InitHelpersCalled;
  197. public string ResultLayoutPage;
  198. public string ResultOverridenLayoutPath;
  199. public ViewContext ResultViewContext;
  200. public string ResultVirtualPath;
  201. public override void Execute()
  202. {
  203. ResultLayoutPage = Layout;
  204. ResultOverridenLayoutPath = OverridenLayoutPath;
  205. ResultViewContext = ViewContext;
  206. ResultVirtualPath = VirtualPath;
  207. }
  208. public override void InitHelpers()
  209. {
  210. base.InitHelpers();
  211. InitHelpersCalled = true;
  212. }
  213. }
  214. }
  215. }