Skip to content

Commit c9319c2

Browse files
committed
Updated test to properly evaluate sitemap
The previous test was designed to fail—and expect an exception. That created a false sense of comfort, as the core functionality wasn't actually being tested. This issue is resolved, and the test has been updated to look for `utf-8` encoding, along with a `standalone` attribute. The former is required by sitemap.org's schema. The latter will be written by `XDocument` serialization, assuming we continue to use `XDeclaration`.
1 parent a83b844 commit c9319c2

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

OnTopic.AspNetCore.Mvc.Tests/TopicControllerTest.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,24 @@ public void RedirectController_TopicRedirect_ReturnsRedirectResult() {
126126
/// <summary>
127127
/// Triggers the index action of the <see cref="SitemapController.Index()" /> action.
128128
/// </summary>
129-
/// <remarks>
130-
/// Because the <see cref="SitemapController.Index()"/> method references the <see cref="Controller.Response"/> property,
131-
/// which is not set during unit testing, this test is <i>expected</i> to throw an exception. This is not ideal. In the
132-
/// future, this may be modified to instead use a mock <see cref="ControllerContext"/> for a more sophisticated test.
133-
/// </remarks>
134129
[TestMethod]
135-
[ExpectedException(typeof(NullReferenceException), AllowDerivedTypes=false)]
136130
public void SitemapController_Index_ReturnsSitemapXml() {
137131

138-
var controller = new SitemapController(_topicRepository);
139-
var result = controller.Index() as ViewResult;
140-
var model = result.Model as string;
132+
var actionContext = new ActionContext {
133+
HttpContext = new DefaultHttpContext(),
134+
RouteData = new RouteData(),
135+
ActionDescriptor = new ControllerActionDescriptor()
136+
};
137+
var controller = new SitemapController(_topicRepository) {
138+
ControllerContext = new ControllerContext(actionContext)
139+
};
140+
var result = controller.Index() as ContentResult;
141+
var model = result.Content as string;
141142

142143
controller.Dispose();
143144

144145
Assert.IsNotNull(model);
145-
Assert.IsTrue(model.StartsWith("<?xml version=\"1.0\" encoding=\"utf-16\"?>"));
146+
Assert.IsTrue(model.StartsWith("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>"));
146147
Assert.IsTrue(model.Contains("/Web/Web_1/Web_1_1/Web_1_1_1/</loc>"));
147148

148149
}

0 commit comments

Comments
 (0)