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 wil be using Rhino Mocks. I am going to use Visual Studio 2015 for the development. I hope you will like this article.
Download source code
WCF_NUnit_Tests_Rhino_Mocks.zip
Background
As a developer, we all writes lots of codes in our day to day life. Am I right? It is more important to check whether the codes we have written works well. So for that we developer usually do unit testing, few developers are doing a manual testing to just check whether the functionality is working or not. I would say that it is wrong. In a TDD (Test Driven Development) unit testing is very important, where we actually writes the test cases before we start our coding. Let us see what exactly the “Unit Tesing” is.
Unit Testing
Unit testing is the process of testing a unit, it can be a class, a block of codde, function, a property. We can easily test our units independently. In dot net we have so many frameworks to do unit testing. But here we are going to use NUnit which I found very easy to write tests.
If you have Resharper installed in your machine, it will be more easier to execute and debug your tests. Here I am using Resharper in my Visual Studio, so the screenshots will be based on that. Thank you
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.
empty_project
Now, we will add a WCF Service as follows.
create_a_wcf_service
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 to read some basics here.
Now, it is time to set up our database and insert some data.
Creating database
Here I am creating a database with name “TrialDB”, you can always create a DB by running the query given below.
Create a table and insert data in database
To create a table, you can run the query below.
Now we can insert few data to our newly created table.
So our data is ready, that means we are all set to write our service and tests. Now go to your solution and create an entity data model.
entity_framework
So entity is also been created. Now please open your interface and that is where we start our coding. We can change the interface as follows.
Here we have created two operations, one to get a course by id and one to retrieve all the courses as a list. Now please go and implement these two operations in our service file. You can modify that class as follows.
In the above code, as you can see we are creating two constructor one is without parameter and other is with parameter, and we are having IMyService as a parameter. In this way we can achieve the dependency injection when we write tests for our unit. So what we need to do all is, just pass the dependency, in this case it is IMyService.
In software engineering, dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. Source: WikiPedia
If you need to know more on dependency injection, please read here. Now we will build and check whether our service is working fine or not. Please press CTRL+F5.
invoking_wcf_service
As our services are ready, we can now create the tests for those operations. For that we can create a new class library in our project and name it UnitTest.Service. Please add a class MyServiceTests in the class library where we can add our tests. And please do not forget to add our application reference too.
add_project_reference
Installing and configuring NUnit
Now we can install NUnit to our test project from NuGet Package. Once you add the package, you will be able to add the preceding namespace in our MyServiceTests class.
In NUnit we have so many attributes that can be used for different purposes, but now we are going to use only four among them.
TestFixture
testfixture_in_nunit
OneTimeSetUp
one_time_setup_attribute_in_nunit
In previous versions, we were using TestFixtureSetUp, as the TestFixtureSetUp is obsolete, now we are using OneTimeSetUp
testfixturesetup_attribute_is_obsolete
TearDown
This attribute is used to identify a method that is called immediately after each tests, it will be called even if there is any error, this is the place we can dispose our objects.
Test
This attribute is used to make a method callable from NUnit test runner. This can not be inherited.
Now we can see all these attributes in action. So let us write some tests, but the real problem is we need to mock the IMyService right as the parameterized constructor of the class MyService expecting it. Remember, we have discussed about setting up our services in the way which can be injected the dependencies? No worries, we can install Rhino Mock for that now.
rhino_mocks_in_nuget_package
So we can add the tests are dependencies as follows in our test class.
As you can see we have mocked our IMyService as follows.
generate_mock_with_rhino
And, in the test GetCourseById_Return_NotNull_Pass we have also used a method called Stub. Stub actually tell the mock object to perform a certain action when a matching method is called, and it doesn’t create an expectation for the same. So you might be thinking, how can we create an expectation? For that we have a method called Expect.
expect_in_rhino_mock
It is always recommended to verify your expectation when you use Expect as we used it in our test GetAllCourses_Return_List_Count_Pass.
As I already said, I am using Resharper, we have so many shortcuts to run our tests, now if you right click on your TestFixture. You can see a run all option as preceding.
run_all_test_option_in_resharper
As I was getting an error as “No connection string named ‘Entity’ could be found in the application config file.” when I run the tests, I was forced to install the entity framework in my test project and also add a new config file with the connection string like we have in our web config file.
If everything goes fine and you don’t have any errors, I am sure you will get a screen as preceding.
nunit_output
Happy coding!.
See also
Walkthrough: Creating and Running Unit Tests for Managed Code
NUnit
Rhino Mocks
Conclusion
Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
Your turn. What do you think?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.
Kindest Regards
Sibeesh Venu