Getting started with Unit Testing and Moq - Part 3

kritner profile image Russ Hammett Originally published at kritner.blogspot.com on ・4 min read

Part 1

Part 2

Part 3 you are here

Part 4

In the previous post we got our first tests set up with moq. So now let’s see about creating some unit tests to go along with our WCF services.

First thing we need to do, is create our WCF service. Nothing fancy, the default functions created with the WCF Application should suffice.

First, add a new project to the RussUnitTestSample solution:

Right click solution -> Add -> New Project...

WCF -> Wcf Service Application. Named RussUnitTestSample.Wcf

Your newly added project should look similar to:

Next, we’ll want to configure our project to have multiple start up projects (both the console app, and the wcf service), additionally we will add the WCF service as a service reference in the console application.

Multiple startup projects. Right click solution -> Properties

Select multiple startup projects, change the combo boxes so both the console application, and the wcf application are set to “start”

Find the port the WCF service is set to run on next. Right click the WCF project -> properties

Copy the URL highlighted for use in the next step

Next, we’ll add the WCF project as a service reference to the console application.

In the console application, Right click “References” -> Add Service Reference...

Paste the URL copied before (localhost:portNumber/) -> Discover -> Drill into

service and select your interface (IService1)

Your project should now display a new Folder “Service References” and have the service reference listed. Additionally notice my app.config file has been automatically checked out, as WCF endpoint information has been added.

Here is what was added to the config file

            <binding name="BasicHttpBinding\_IService1" />
        <endpoint address="http://localhost:23336/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding\_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding\_IService1" />

Now our service reference is all added and ready, let’s test it! Modify the Program.CS of the console application with:

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

Console.WriteLine("{0}", client.GetData(42));

Your program.cs should now look like:

namespace RussUnitTestSample
    class Program

        #region consts
        const string CONNECTION\_STRING = "Data Source=,1515;Initial Catalog=MBES;Persist Security Info=True;Integrated Security=true;";
        #endregion consts

        #region Entry

        static void Main(string[] args)
            GetNumbersAndAddThem obj = new GetNumbersAndAddThem(
                new DbGetSomeNumbers(new BaseDbConnection(CONNECTION\_STRING)),
                new NumberFunctions()


            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

            Console.WriteLine("{0}", client.GetData(42));


        #endregion Entry

Give it a run and:

Now our WCF service is hosted, and successfully consumed in the console application. Diff in previous and current can be found on GitHub, note a lot of files in the pull request are auto-generated from the WCF service.

Next, we’ll look at how to test it.

Posted on by:

kritner profile

Russ Hammett


Dad, programmer, faux-chef, gamer, book reader


markdown guide