Use RazorEngine to perform UnitTest on Views of ASP. net mvc,

Source: Internet
Author: User

Use RazorEngine to perform UnitTest on Views of ASP. net mvc,

Sometimes we need to perform unit tests on the final text produced by Razor (html or xml or.

Use Nuget to install RazorEngine.

Create an 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 \ 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 );}}}}

Result:

 

Bingo!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.