Unit test using HtmlHelper in ASP.NET Core
Asked Answered
S

0

6

Although there are tag helpers now, you still need HtmlHelper from time to time, e.g., inside your tag helpers. I'm trying to write tests for one of my tag helpers which calls into HtmlHelper.

In ASP.NET MVC 5 there have been ways to create an HtmlHelper for testing purposes, e.g., see https://mcmap.net/q/276151/-unit-testing-htmlhelper-extension-method-fails

However, the ctor changed extensively in ASP.NET Core.

What is a good way to create an HtmlHelper for testing purposes using ASP.NET Core 1.1?

Sami answered 3/8, 2017 at 6:28 Comment(3)
Can you abstract your tag helper to use the IHtmlHelper interface? That can be injected into the constructor of the tag helper, which makes mocking trivial.Gormandize
We want to write integration tests, so I'm looking for a way to really instantiate an HtmlHelper instead of faking it.Sami
See also github.com/aspnet/AspNetCore/issues/4843 for more information on how we "solved" the issue.Sami

© 2022 - 2024 — McMap. All rights reserved.