DEV Community

Russ Hammett
Russ Hammett

Posted on • Originally published at kritner.blogspot.com on

Getting started with Unit Testing and Moq - Part 3

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


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

Enter fullscreen mode Exit fullscreen mode

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("\n");
Console.WriteLine("{0}", client.GetData(42));
Console.WriteLine("\n");

Enter fullscreen mode Exit fullscreen mode

Your program.cs should now look like:

namespace RussUnitTestSample
{
    class Program
    {

        #region consts
        const string CONNECTION\_STRING = "Data Source=192.168.50.4,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()
            );

            Console.WriteLine("\n");
            Console.WriteLine(obj.Execute());
            Console.WriteLine("\n");

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

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

        }

        #endregion Entry
    }
}

Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)