diff --git a/samples/TagHelperSample.Web/Controllers/HomeController.cs b/samples/TagHelperSample.Web/Controllers/HomeController.cs index d9aaf0694c..3d9f388d65 100644 --- a/samples/TagHelperSample.Web/Controllers/HomeController.cs +++ b/samples/TagHelperSample.Web/Controllers/HomeController.cs @@ -17,11 +17,11 @@ namespace TagHelperSample.Web.Controllers public HomeController() { - // Unable to set ViewBag (or ViewData) entries from constructor due to an InvalidOperationException thrown + // Unable to set ViewBag or ViewData entries from constructor due to an InvalidOperationException thrown // from the DynamicViewData.ViewData getter. In MVC 5.2, no properties in the Controller class except // ControllerContext, Url, and anything ControllerContext-derived (e.g. HttpContext and User) return null // even if invoked from the constructor i.e. prior to the Initialize() call. - ////ViewBag.Items = _items; + ////ViewData["Items"] = _items; } // GET: // @@ -33,7 +33,7 @@ namespace TagHelperSample.Web.Controllers // GET: /Home/Create public IActionResult Create() { - ViewBag.Items = _items; + ViewData["Items"] = _items; return View(); } @@ -49,7 +49,7 @@ namespace TagHelperSample.Web.Controllers return RedirectToAction("Index"); } - ViewBag.Items = _items; + ViewData["Items"] = _items; return View(); } @@ -59,7 +59,7 @@ namespace TagHelperSample.Web.Controllers User user; _users.TryGetValue(id, out user); - ViewBag.Items = _items; + ViewData["Items"] = _items; return View(user); } @@ -73,8 +73,18 @@ namespace TagHelperSample.Web.Controllers return RedirectToAction("Index"); } - ViewBag.Items = _items; + ViewData["Items"] = _items; return View(); } + + // POST: Home/Reset + [HttpPost] + public IActionResult Reset() + { + _users.Clear(); + _next = 0; + + return RedirectToAction("Index"); + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperSampleTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperSampleTest.cs index 09eb48052c..327919a07c 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperSampleTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelperSampleTest.cs @@ -9,7 +9,6 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; using System.Threading.Tasks; -using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing.xunit; using Xunit; @@ -29,6 +28,23 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public async Task HomeController_Index_ReturnsExpectedContent() { // Arrange + var resetResponse = await Client.PostAsync("http://localhost/Home/Reset", content: null); + + // Guard 1 (start from scratch) + AssertRedirectsToHome(resetResponse); + + var createBillyContent = CreateUserFormContent("Billy", "2000-11-28", 0, "hello"); + var createBillyResponse = await Client.PostAsync("http://localhost/Home/Create", createBillyContent); + + // Guard 2 (ensure user 0 exists) + AssertRedirectsToHome(createBillyResponse); + + var createBobbyContent = CreateUserFormContent("Bobby", "1999-10-27", 1, "howdy"); + var createBobbyResponse = await Client.PostAsync("http://localhost/Home/Create", createBobbyContent); + + // Guard 3 (ensure user 1 exists) + AssertRedirectsToHome(createBobbyResponse); + var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); var outputFile = "compiler/resources/TagHelperSample.Web.Home.Index.html"; var resourceAssembly = typeof(TagHelperSampleTest).GetTypeInfo().Assembly; @@ -49,6 +65,34 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests #endif } + [Fact] + public async Task HomeController_Index_ReturnsExpectedContent_AfterReset() + { + // Arrange + var resetResponse = await Client.PostAsync("http://localhost/Home/Reset", content: null); + + // Guard (start from scratch) + AssertRedirectsToHome(resetResponse); + + var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); + var outputFile = "compiler/resources/TagHelperSample.Web.Home.Index-Reset.html"; + var resourceAssembly = typeof(TagHelperSampleTest).GetTypeInfo().Assembly; + var expectedContent = await ResourceFile.ReadResourceAsync(resourceAssembly, outputFile, sourceFile: false); + + // Act + var response = await Client.GetAsync("http://localhost/"); + var responseContent = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + +#if GENERATE_BASELINES + ResourceFile.UpdateFile(resourceAssembly, outputFile, expectedContent, responseContent); +#else + Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true); +#endif + } [Fact] public async Task HomeController_Create_Get_ReturnsSuccess() @@ -80,7 +124,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests var createBillyContent = CreateUserFormContent("Billy", "2000-11-30", 0, "hello"); var createBilly = await Client.PostAsync("http://localhost/Home/Create", createBillyContent); - // Guard + // Guard (ensure user 0 exists) AssertRedirectsToHome(createBilly); // Act @@ -95,12 +139,13 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { // Arrange var createBillyContent = CreateUserFormContent("Billy", "2000-11-30", 0, "hello"); - var changeBillyContent = CreateUserFormContent("Bobby", "1999-11-30", 1, "howdy"); var createBilly = await Client.PostAsync("http://localhost/Home/Create", createBillyContent); - // Guard + // Guard (ensure user 0 exists) AssertRedirectsToHome(createBilly); + var changeBillyContent = CreateUserFormContent("Bobby", "1999-11-30", 1, "howdy"); + // Act var changeBilly = await Client.PostAsync("http://localhost/Home/Edit/0", changeBillyContent); @@ -108,6 +153,16 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests AssertRedirectsToHome(changeBilly); } + [Fact] + public async Task HomeController_Reset_ReturnsSuccess() + { + // Arrange and Act + var response = await Client.PostAsync("http://localhost/Home/Reset", content: null); + + // Assert + AssertRedirectsToHome(response); + } + public static TheoryData MoviesControllerPageData { get diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index-Reset.html b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index-Reset.html new file mode 100644 index 0000000000..23fa0b6c19 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index-Reset.html @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + +

Index

+

+ Create New +

+ + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html index 7e2e8ddcd0..036014f887 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/compiler/resources/TagHelperSample.Web.Home.Index.html @@ -33,7 +33,7 @@
- +
@@ -49,20 +49,20 @@ hello

- +
- +
- +
+howdy

Edit