I had two small “breakthroughs” while coding today that saved me quite a bit of time. The first dealt with mocking HtmlHelper<T> with Moq for unit testing purposes:
/// <summary> /// A mocked-out <see cref="HtmlHelper{TModel}"/> that can be used for testing. /// </summary> /// <typeparam name="TModel">The view model type.</typeparam> public class FakeHtmlHelper<TModel> : HtmlHelper<TModel> where TModel : class { /// <summary> /// Creates the fake. /// </summary> public FakeHtmlHelper(TModel model) : base(new FakeViewContext(), GetMockContainer(model)) { } /// <summary> /// Gets a mock view data container that returns view data for the specified model. /// </summary> /// <param name="model"></param> /// <returns></returns> private static IViewDataContainer GetMockContainer(TModel model) { var dataContainer = new Mock<IViewDataContainer>(); ViewDataDictionary<TModel> dataDictionary = new ViewDataDictionary<TModel>(model); dataContainer.Setup(c => c.ViewData).Returns(dataDictionary); return dataContainer.Object; } }
(Note that FakeViewContext is a similarly-designed fake class, but it could also be a Mock object.)
With this fake helper, you can now unit test extension methods for HtmlHelper<T>.
The second “snippet” I found useful today was aligning a group of divs. The desired layout was something like the following:
However, setting the divs to display to inline-block resulted in something like this instead, where the div with less content did not line up with the other divs even though their heights were all set to the same value:
A quick scan of the CSS spec got me pointed back in the right direction, and I used a vertical-align of top to achieve the desired layout.
Great post, you make good points in a succinct and suitable fashion. I will right away grab your rss feed to stay informed of any updates, thank you for your time!
Hello blogger, thank you very much for sharing this information! I found it so cool. Best regards, Penolope!!
For some reason my browser doesn’t display this page correctly… However, it was a really interesting article, keep up the good work and I’ll be back soon 🙂
<P>Hey, This is a superb thread. I found you on yahoo. Keep up the work.</P>