SparkyTestHelpers: ASP.NET MVC Core Razor Pages

Brian Schroer
Mar 27, 2018 · 2 min read
photo by RaSeBo

PageModelTester<TPageModel>

using SparkyTestHelpers.AspNetMvcvar homeModel = new HomeModel(/* with test dependencies */);
var pageTester = new PageModelTester<HomeModel>(homeModel);

PageModelActionTester<TPageModel>

var actionTester = 
new PageModelTester<HomeModel>(homeModel).Action(x => x.OnGet);
var homeModel = new HomeModel(/* with test dependencies */);
var pageTester = new PageModelTester<HomeModel>(homeModel);
pageTester
.Action(x => x.OnGet)
.ExpectingModel(model => Assert.IsTrue(model.Foo))
.TestPage();
pageTester
.Action(x => x.OnPost)
.WhenModelStateIsValidEquals(false)
.ExpectingModel(model =>
Assert.AreEqual(expectedErrorMessage, model.ErrorMessage))
.TestPage();
pageTester
.Action(x => x.Post)
.WhenModelStateIsValidEquals(true)
.TestRedirectToPage("UpdateSuccessful");

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade