Moq Testing Tutorial

Posted On: December 1st, 2009

If your reading this then I assume you are either looking for or have an interest in a mocking framework for testing your .NET Applications.

In this tutorial I will step you through a framework called Moq for (.NET) released under the New BSD License. I will not be discussing what Mocking is here as that’s outside the scope of this tutorial. What is mocking/mock object?

Before we jump in to the code, here are some important links:

Get Moq.

Get Moq Contrib

Moq API.

So lets dive in, assuming you already added a reference to the Moq DLL and Moq.Contrib DLL in your project. Let’s start out by creating a new Abstract class to your project. During this session I am going to call this class: UnitBaseAbstractClass for readability.

, , , , , ,

Consume a WordPress RSS Feed in C# and cache it.

Posted On: April 1st, 2009

I couldn’t find a simple example of reading a WordPress Blog Feed in C# and caching it. So I decided to write out my own.

Here is an example to get you started. I wrote it in two parts:

A Class that Consumes a Feed (FeedReader)
An Object representing a Feed Item (FeedViewModel).

And so we begin:

, , , , , ,

ASP.NET MVC Email Template Solution

Posted On: March 1st, 2009

If you are like me then you have probably ran into a wall with using ASP.NET MVC View Engine to render your awesome Dynamic HTML Email Templates. At least you put up a fight using a lot of fake data. It is actually possible to get around it!


        var oldContext = HttpContext.Current;
        HttpContext.Current = new HttpContext(HttpContext.Current.Request,
        fakeResponse);

        var html = new HtmlHelper(new ViewContext(fakeControllerContext,
        new FakeView(), fakeViewDataDictionary, new TempDataDictionary()),
        new ViewPage());

        html.RenderPartial(viewName, viewData, viewDataDictionary);
        HttpContext.Current = oldContext; //yay 

Though, because you’re reading this you’re like me and you never give up! So how, how do you get around all of this? The answer is Spark. Eeek, another DLL? Yes, Spark is its own View Engine which is an entire different framework. Yes, you have to go to their website download the DLLS, put it in your Lib folder and reference it in your code. Let me tell you now, it is well worth it. Let’s dive in!

Let’s write the code to render our email using the Spark. We want our code to take in the template name, something like: “ForgotUsernameEmail.spark” and we also want it to reuse our current ViewData Object which is of type ViewDataDictionary and contains Person data, we will need to add this into our own implementation of AbstractSparkView. See samples bellow:

, , , , , , , , , , ,