Sometimes we need to razor the last produced text (HTML or XML or. ) for unit testing.
Install Razorengine using NuGet.
Create a new ASP. NET MVC project with a test project.
Modify Index.cshtml
@using Testrazor.models@model Testmodel<div>this is a test page</div><div>name @Model. Name </div ><div>age @Model .age</div>
Test:
[TestClass] public class Homecontrollertest {[TestMethod] public void Index () {var config = new templateserviceconfiguration (); Config. Debug = true; using (StreamReader sr = File.OpenText (@ "E:\ temporary \testrazor\testrazor\views\home\index.cshtml")) { var template = Sr. ReadToEnd (); using (var service = razorengineservice.create (config)) {var html = service. Runcompile (template, "test", NULL, new Testmodel {Name = "Kklldog", Age = 10}); Assert.istrue (HTML. Contains ("This is a test page"); Assert.istrue (HTML. Contains ("Name Kklldog")); Assert.istrue (HTML. Contains ("Age 10")); Console.WriteLine (HTML); } } } }
Results:
Bingo!
Use Razorengine to UnitTest views of ASP.