Getting started with Unit Testing and Moq - Part 3
Tags: net, aspnet, csharp, development
Published: 2018-08-28 20:00:15

---
title: Getting started with Unit Testing and Moq - Part 3
published: true
tags: .net,asp.net,csharp,development
canonical_url: https://kritner.blogspot.com/2015/12/getting-started-with-unit-testing-and.html
---

[Part 1](http://kritner.blogspot.com/2015/11/getting-started-with-unit-testing-and.html)  
[Part 2](http://kritner.blogspot.com/2015/12/in-previous-post-i-started-on-project.html)  
[Part 3](http://kritner.blogspot.com/2015/12/getting-started-with-unit-testing-and.html) you are here  
[Part 4](http://kritner.blogspot.com/2016/01/getting-started-with-unit-testing-and.html)  
  
In the [previous post](http://kritner.blogspot.com/2015/12/in-previous-post-i-started-on-project.html) 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](https://msdn.microsoft.com/en-us/library/ms731082(v=vs.110).aspx) 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...  
  
[![](https://4.bp.blogspot.com/-wcI8OAVYPqE/VnIDBRi-lvI/AAAAAAAACxU/PDUlIFw2ZZY/s400/00.png)](http://4.bp.blogspot.com/-wcI8OAVYPqE/VnIDBRi-lvI/AAAAAAAACxU/PDUlIFw2ZZY/s1600/00.png)  
  
WCF -> Wcf Service Application.  Named RussUnitTestSample.Wcf  
  
[![](https://4.bp.blogspot.com/-vILtrJq7mAk/VnIDEuFBIQI/AAAAAAAACxg/cKFX-GWOfC4/s400/01.png)](http://4.bp.blogspot.com/-vILtrJq7mAk/VnIDEuFBIQI/AAAAAAAACxg/cKFX-GWOfC4/s1600/01.png)  
  
Your newly added project should look similar to:  
  
[![](https://4.bp.blogspot.com/-55CquCag-v4/VnIDKgNBvlI/AAAAAAAACxs/kEal6XVQH7g/s400/02.png)](http://4.bp.blogspot.com/-55CquCag-v4/VnIDKgNBvlI/AAAAAAAACxs/kEal6XVQH7g/s1600/02.png)  
  
  
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  
  
[![](https://2.bp.blogspot.com/-79D3wzDIm78/VnIDQeJM_RI/AAAAAAAACx4/BbgQQwkFjek/s400/03.png)](http://2.bp.blogspot.com/-79D3wzDIm78/VnIDQeJM_RI/AAAAAAAACx4/BbgQQwkFjek/s1600/03.png)  
  
  
Select multiple startup projects, change the combo boxes so both the console application, and the wcf application are set to “start”  
  
[![](https://4.bp.blogspot.com/-viyGBtNL4BM/VnIDYHxTQAI/AAAAAAAACyQ/KUyq790JTsk/s400/04.png)](http://4.bp.blogspot.com/-viyGBtNL4BM/VnIDYHxTQAI/AAAAAAAACyQ/KUyq790JTsk/s1600/04.png)  
  
  
Find the port the WCF service is set to run on next.  Right click the WCF project -> properties  
  
[![](https://2.bp.blogspot.com/-QqPwE855qOE/VnIDc88yugI/AAAAAAAACyc/NLEFu0AMn0M/s400/05.png)](http://2.bp.blogspot.com/-QqPwE855qOE/VnIDc88yugI/AAAAAAAACyc/NLEFu0AMn0M/s1600/05.png)  
  
  
Copy the URL highlighted for use in the next step  
  
[![](https://3.bp.blogspot.com/-C-Qz3kAj5CE/VnIDfRYhGnI/AAAAAAAACyo/dIdMzXM0Dng/s400/06.png)](http://3.bp.blogspot.com/-C-Qz3kAj5CE/VnIDfRYhGnI/AAAAAAAACyo/dIdMzXM0Dng/s1600/06.png)  
  
  
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...  
  
[![](https://1.bp.blogspot.com/-5a03T2pEXR4/VnIDiwxhXwI/AAAAAAAACy0/I6rSWIKGo-M/s400/07.png)](http://1.bp.blogspot.com/-5a03T2pEXR4/VnIDiwxhXwI/AAAAAAAACy0/I6rSWIKGo-M/s1600/07.png)  
  
  
Paste the URL copied before (localhost:portNumber/) -> Discover -> Drill into  
service and select your interface (IService1)  
  
[![](https://1.bp.blogspot.com/-_MkWPi3tHH4/VnIDkhyW5mI/AAAAAAAACzA/3f-apHCPuZ4/s400/08.png)](http://1.bp.blogspot.com/-_MkWPi3tHH4/VnIDkhyW5mI/AAAAAAAACzA/3f-apHCPuZ4/s1600/08.png)  
  
  
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.  
  
[![](https://2.bp.blogspot.com/--RBfMg6NaeU/VnIDmwrGlAI/AAAAAAAACzM/N-Un83K8urs/s400/09.png)](http://2.bp.blogspot.com/--RBfMg6NaeU/VnIDmwrGlAI/AAAAAAAACzM/N-Un83K8urs/s1600/09.png)  
  
  
Here is what was added to the config file  
  
[![](https://4.bp.blogspot.com/-lEriOZSZxc4/VnIDpKuX1HI/AAAAAAAACzY/fspJ5JDgooI/s400/10.png)](http://4.bp.blogspot.com/-lEriOZSZxc4/VnIDpKuX1HI/AAAAAAAACzY/fspJ5JDgooI/s1600/10.png)  
  



```xml
<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>

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

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

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

```
  
Your program.cs should now look like:  



```cs
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
    }
}

```
Give it a run and:  
  
[![](https://4.bp.blogspot.com/-aRv_yZ_saMk/VnIDt3VESfI/AAAAAAAACzk/pBRSmI3QeKk/s400/11.png)](http://4.bp.blogspot.com/-aRv_yZ_saMk/VnIDt3VESfI/AAAAAAAACzk/pBRSmI3QeKk/s1600/11.png)  
  
Now our WCF service is hosted, and successfully consumed in the console application.  Diff in previous and current can be found on [GitHub](https://github.com/Kritner/RussUnitTestSample/commit/23c08678d314a3f53df5c28a53ad4ecdeaa108cc), 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.