Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:18:54+00:00 2026-05-30T11:18:54+00:00

I have a HtmlHelper which is used to output some accordion style functionality. This

  • 0

I have a HtmlHelper which is used to output some accordion style functionality. This helper is going to be used extensively throughout the site.

The code used to create this functionality makes extensive use of TagBuilder and of course we are outputting the html using:

htmlHelper.ViewContext.Writer.WriteLine(someTag.ToString(TagRenderMode.StartTag));

We have a few helpers which use this pattern and it’s working great.

All good so far – it does exactly what I want. However – testing this is proving to be a nightmare. how do i test htmlHelper.ViewContext.Writer.WriteLine ?

we’re using xUnit and Moq if it matters

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:18:55+00:00Added an answer on May 30, 2026 at 11:18 am
    var viewContext = new ViewContext();
    var viewContextWriter = new Mock<TextWriter>();
    var text = string.Empty;
    viewContextWriter.Setup(m => m.WriteLine(It.IsAny<string>()))
        .Callback((string s) => text += s);
    viewContext.Writer = viewContextWriter.Object;
    var html = new HtmlHelper(viewContext, new FakeViewDataContainer());
    
    private class FakeViewDataContainer : IViewDataContainer
    {
        private ViewDataDictionary _viewData = new ViewDataDictionary();
        public ViewDataDictionary ViewData
        {
            get { return _viewData; }
            set { _viewData = value; }
        }
    }
    

    When you assert, whatever is written out to Writer.WriteLine will be in the text method’s text variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my own html helper extension, which I use this way <%=Html.LocalizableLabelFor(model =>
I have a HtmlHelper extension method that I would like to apply some logic
Is there a better way to do this? I have an HTML helper extension
Ive got this function as a html helper which should write a javascript function
I have created an HtmlHelper Extension method which returns an encoded string, I have
I have helper method which is only checking if User is Authenticated but its
so here it goes: I have a html helper which renders ActionLink s with
I have a large view that needs some conditional logic to decide which of
I have an HtmlHelper function that returns a MvcHtmlString and which I'd like to
I'm trying to write a strongly typed helper which would be something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.