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 |
|
Now our service reference is all added and ready, let’s test it! Modify the Program.cs
of the console application with:
1 |
|
Your Program.cs
should now look like:
1 |
|
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.
Getting Started with Unit Testing and Moq Part 3
https://blog.kritner.com/2015/12/21/Getting-Started-with-Unit-Testing-and-Moq-Part-3/