0

I'm working on my own html helper. And now I'd like to test it. The way I chose is using the RazorEngine library to compile razor code and check a result.

Well I've started with the sample like below:

string template = "<div>@Html.Raw('sdf')</div>";
Engine.Razor.AddTemplate("mytemplate", template);
var r = Engine.Razor.RunCompile("mytemplate");

But I've got the error in this case - Additional information: Errors while compiling a Template.

I've found this question RazorEngine issues with @Html. But it looks too old.

Community
  • 1
  • 1
Sergey
  • 5,396
  • 3
  • 26
  • 38

2 Answers2

0

Html.Raw is not a part of Razor. You can use @Raw instead

Example:

var template = "<div>@Html.Raw(\"<strong>Bold!</strong>\")</div>";
template = template.Replace("@Html.Raw", "@Raw");
var r = Engine.Razor.RunCompile(template, "templateKey");
Tom Baires
  • 381
  • 3
  • 16
0

What exactly are you trying to test here - what is your html helper doing?

Performing a unit test is quite straight forward for html helpers, the HtmlHelper class takes in a ViewContext class to which you can access the stream writer that gets written to when calling your method. All you need to do is test against this with your expected outcome.

No need to create RazorViewEngine instances, as you are testing a much larger scope than what you need.

This is what you need to be doing: https://www.danylkoweb.com/Blog/the-ultimate-guide-to-unit-testing-in-aspnet-mvc-E2