Getting Started with Unit Testing and Moq Part 3

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.

Part 1
Part 2
Part 3 you are here
Part 4

Nothing really fancy for our WCF service, 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

1
2
3
4
5
6
7
8
9
10
11
12
<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:

1
2
3
4
5
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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:

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.

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×