In this post, we will see how we can write unit test cases for our WCF Service, with a framework called NUnit. We will also be covering how to mock our dependencies in our test, here we will be using Rhino Mocks. I am going to use Visual Studio 2015 for the development. I hope you will like this article.
Please see this article in my blog here.
Download source code
Background
As a developer, we all write lots of codes in our day to day life. It is more important to check whether the codes, we have written, work well or not. So for that, we developers usually use unit testing. Only a few developers are still practicing manual testing to just check whether the functionality is working or not. I would say that is wrong. In TDD (Test Driven Development), unit testing is very important, where we actually write the test cases before we start our coding. Let us see what exactly the “Unit Testing” is.
Unit Testing
Unit testing is the process of testing a unit, it can be a class, a block of code, a function, or a property. We can easily test our units independently. In .NET, we have so many frameworks to perform the unit testing but here, we are going to use NUnit in which I found very easy to write tests.
If you have Resharper installed in your machine, it will be easier to execute and debug your tests. Here, I am using Resharper in my Visual Studio, so the screenshots will be based on that.
Now, it is time to set up our project and start our coding.
Setting up the project
To get started, please create an empty project in your Visual Studio.

Now, we will add a WCF Service, as follows.

Once you are done, you can see two files, an Interface(IMyService) and a class (MyService) with .svc extension. If you are completely new to WCF service, I strongly recommend you read some basics here.
Now, it is time to set up our database and insert some data.
Creating a database
Here, I am creating a database with the name “TrialDB”, you can always create a DB by running the query given below.
- USE[master]
- GO
- /****** Object: Database [TrialDB] Script Date: 20-11-2016 03:54:53 PM ******/
- CREATE DATABASE[TrialDB]
- CONTAINMENT = NONE
- ON PRIMARY(NAME = N 'TrialDB', FILENAME = N 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\TrialDB.mdf', SIZE = 8192 KB, MAXSIZE = UNLIMITED, FILEGROWTH = 65536 KB)
- LOG ON(NAME = N 'TrialDB_log', FILENAME = N 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\TrialDB_log.ldf', SIZE = 8192 KB, MAXSIZE = 2048 GB, FILEGROWTH = 65536 KB)
- GO
- ALTER DATABASE[TrialDB] SET COMPATIBILITY_LEVEL = 130
- GO
- IF(1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC[TrialDB].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE[TrialDB] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE[TrialDB] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE[TrialDB] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE[TrialDB] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE[TrialDB] SET ARITHABORT OFF
- GO
- ALTER DATABASE[TrialDB] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE[TrialDB] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE[TrialDB] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE[TrialDB] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE[TrialDB] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE[TrialDB] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE[TrialDB] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE[TrialDB] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE[TrialDB] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE[TrialDB] SET DISABLE_BROKER
- GO
- ALTER DATABASE[TrialDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE[TrialDB] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE[TrialDB] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE[TrialDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE[TrialDB] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE[TrialDB] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE[TrialDB] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE[TrialDB] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE[TrialDB] SET MULTI_USER
- GO
- ALTER DATABASE[TrialDB] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE[TrialDB] SET DB_CHAINING OFF
- GO
- ALTER DATABASE[TrialDB] SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF)
- GO
- ALTER DATABASE[TrialDB] SET TARGET_RECOVERY_TIME = 60 SECONDS
- GO
- ALTER DATABASE[TrialDB] SET DELAYED_DURABILITY = DISABLED
- GO
- ALTER DATABASE[TrialDB] SET QUERY_STORE = OFF
- GO
- USE[TrialDB]
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
- GO
- ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
- GO
- ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
- GO
- ALTER DATABASE[TrialDB] SET READ_WRITE
- GO
To create a table, you can run the query below.
- USE[TrialDB]
- GO
- /****** Object: Table [dbo].[Course] Script Date: 20-11-2016 03:57:30 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE[dbo].[Course](
- [CourseID][int] NOT NULL, [CourseName][nvarchar](50) NOT NULL, [CourseDescription][nvarchar](100) NULL, CONSTRAINT[PK_Course] PRIMARY KEY CLUSTERED(
- [CourseID] ASC) WITH(PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON[PRIMARY]) ON[PRIMARY]
- GO
- Now we can insert few data to our newly created table.
- USE[TrialDB]
- GO
- INSERT INTO[dbo].[Course]
- ([CourseID], [CourseName], [CourseDescription])
- VALUES(1, 'C#', 'Learn C# in 7 days')
- INSERT INTO[dbo].[Course]
- ([CourseID], [CourseName], [CourseDescription])
- VALUES(2, 'Asp.Net', 'Learn Asp.Net in 7 days')
- INSERT INTO[dbo].[Course]
- ([CourseID], [CourseName], [CourseDescription])
- VALUES(3, 'SQL', 'Learn SQL in 7 days')
- INSERT INTO[dbo].[Course]
- ([CourseID], [CourseName], [CourseDescription])
- VALUES(4, 'JavaScript', 'Learn JavaScript in 7 days')
- GO

Entity has also been created. Now, open your interface, the platform where we start our coding. We can change the interface, as follows.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- namespace WCF_NUnit_Tests_Rheno_Mocks {
- [ServiceContract]
- public interface IMyService {
- [OperationContract]
- Course GetCourseById(int courseId);
- [OperationContract]
- List < Course > GetAllCourses();
- }
- }











Join the conversation! Your thoughts help the community grow.