Skillset

In previous articles, we discussed the classification of application testing and unit testing grounds. We also introduced mock objects and stubs. Today’s article was a challenge for me personally to describe the frameworks to support testing. At the beginning of the explanation, however, we will go in depth into mocks, and only then proceed to the frameworks. Of course, no one is able to describe them all, so I chose the four frameworks that are used every day. Some of them are easy to use, and some of them are really combined to create tests.

Today we will learn to use them and know their advantages and disadvantages. We will learn the three frameworks: Pex, NMock, and Rhino Mocks. So, you ready? What will we begin with? So we’re going to test frameworks. Note! All of the examples in this article will benefit, not eclipse Microsoft Visual Studio.

Mocks again

Let’s begin with mocks. It is a matter of reminding the basic information about them. A mock is really an abstraction of objects or classes, whose task is to effectively isolate the classes, in order to investigate and ensure the appropriate level of verification tests.

Take an example of a data management database. If your class has to use them, and does not want to create the data layer class to support them because of the time that we spend on creating a wrapper for MySQL, create a mock that simulates the retrieved data. In this way we do not need to configure anything or even think about how to write.

We write simply a dummy class and specify the manner in which the target class should behave. Then we simply write tests for the target class. These tests are created based on the expected behavior of the class, which is now a mock. It is one of the solutions that effectively separates all types of classes from each other and at the same time makes the whole process of software development and testing much more pleasant than usual.

So what is really a mock? A mock object is a model of research, which usually is used for research and testing of various interfaces. And so it is ideal for testing and determining the functionality of the individual because the interfaces are mainly used for separating the individual functions from each other. This saves the time that we spend on the implementation of the working classes of data on the interface.

When writing a mock we need to know one thing. First, we must always check the expectations that we have relative to the functionality, then we should check whether the class meets our expectations. A mock is used mostly for classes, where the survey is difficult for some reason. Sometimes mock objects also apply to references to external resources such as web services and databases. Mostly they are solutions that perform too slowly for there to be any point in writing unit tests for them.

An example in which we operate

I want to show you a simple example with which we operate in our project. This example will be based on mock objects and because of it I hope that I can fully convey to you the difference between mocks and stubs. So let’s begin:

First, let’s create a new project in Visual Studio called Examples.

Second, in ConnectionProvider create a class with the following content:

Second, create a class in ChecServerEx. What in this case, should you pay attention to? You must note that in this case ConnectionProviderEx class is not created inside the method IsOnline, instead the instance is injected into the constructor as CheckServerEx IConnectionProviderEx interface. Here’s the code:

In this way we have created our first mock. True, it is not difficult? Now you can go to the frameworks.

NMock3 or start creating mocks object

“NMock3 builds on the work of NMock2. Specifically it adds lambda expressions to the matcher syntax. You can use lambda expressions instead of strings, making refactoring possible. The idea of Syntactic Sugar is maintained in all expectations that read like an English sentence.”

Third, you must note that you should always add “using NMock” to mock the factory that was available.

Fourth, fire up the test, I’m sure will pass.

Pex and Moles, the most interesting of the frameworks for the Net

Pex is the first framework that will be discussed here. It is one of the best tools for testing applications in Visual Studio. It is used to perform automated testing by Whitebox. It is primarily used by applications with NET environment. It was developed by Microsoft in addition to the Visual Studio environment. Pex can be downloaded from http://research.microsoft.com/en-us/projects/pex/downloads.aspx.

Pex can generate tests automatically from the machine that can no longer cover the entire code, known as automated testing. Pex has one major advantage: if Pex finds any potential errors in the code, it suggests how to alter them. This is one of the best features of the service.

Pex also has another interesting feature, namely, Pex is able to perform a complete analysis of the code by searching for boundary conditions and generating test cases. The tool also automatically finds errors and allows you to greatly reduce the maintenance costs of code. Currently Pex and Moles are a total of one tool for creating mocks.

Moles and Pex Installation:

First, if your system does not yet contain Moles and Pex, download and install them.

Second, create a new test project UT.Examples.

Third, look for references to the test project Examples.

Fourth, right-click on the reference from the context menu, select “Add Moles Assembly”. If this option is not available, go back to the first point.

Fifth, you must now rebuild the project Examples.UT.

Sixth you probably noticed in the reference branch, a new reference called “Examples.Moles”. It contains all the implementations of stub objects that are associated with classes Examples project.

Seventh, add a new test IsOnlineTest_MolesStub with the following content:

Eighth, as you probably noticed, adding “using Examples.Moles” to a specific class type SIConnectionProviderEx was available.

Ninth now you can run the test. It is true that it is not too complex?

Well, we described our first test using Moles. The subject, as you see, really is very developed and very complex, and at the same time is very interesting. I hope you enjoyed this introduction to the testing environment Net.

Watch out, though Moles and Pex are your requirements, here they are, taken from Microsoft:

You must have .NET Framework version 3.5 or later

You must have installed Visual Studio 2008 or Visual Studio 2008 Team System

You may also have installed Visual Studio 2010 environment

Of course, the use of Pex or Moles is also a choice. It may happen that you do not have Visual Studio and. NET Framework. Then you can use Pex and Moles on the command line of your operating system. Moles and Pex, despite its unusual requirements, are really not worth it to command, because they are too difficult to use. Soon we will know the real combination.

RhinoMocks, the white rhino of frameworks for testing

I remember how I learned to program using the methodology of TDD (Test Driven Development). When using TDD methodology is very important that the system that we studyis tested in complete isolation. And that’s when I learned to create mocks and stubs. I wanted always to the complete isolation of the system, so that nothing was able to falsify test results. I also needed to be able to simulate some parts of the system. It was then that I met Rhino.

Rhino Mock creates a dynamic dummy for each .NET object. Its main objective is to help the poor developers to create fake implementations of objects and create test interactions between them, and then test them individually. Rhino Mocks can be downloaded from the http://hibernatingrhinos.com/open-source/rhino-mocks.

Now is the time, for example, to create tests using Rhino. Let’s use it for the same test. Here are the steps you need to take:

First, for a project we need to add references Examples.UT library RhinoMocks.dll.

Second, we add to our quality of testing a new test, IsOnlineTest_Rhino:

In total, we’ve covered all the most necessary frameworks. I hope that the examples in the code were not overwhelming. I promise that the next article will discuss Selenium alone, it will be a much lighter subject and you will not feel too overwhelmed by all this. Thanks for your attention.

Adrian Stolarski is a freelance security tech blogger, specializing in Java, PHP, and JQuery. In his own words, he does the hard work of training the unemployed. Currently, he handles Evaluation Visualization for real-time systems with XWT and Eclipse RAP. If he sees that something works, he asks how it works and why it works, then sets out to make it work better. A researcher for InfoSec Institute, he currently lives in Poland, but plans to move to London.

About InfoSec

InfoSec Institute is the best source for high quality information security training. We have been training Information Security and IT Professionals since 1998 with a diverse lineup of relevant training courses. In the past 16 years, over 50,000 individuals have trusted InfoSec Institute for their professional development needs!

Join our newsletter

File download

First Name

Last Name

Work Phone Number

Work Email Address

Job Title

Does your employer pay for training?

What is your timeline for training?

InfoSec institute respects your privacy and will never use your personal information for anything other than to notify you of your requested course pricing. We will never sell your information to third parties. You will not be spammed.

Comments

What is Skillset?

Skillset

Practice tests & assessments.

Practice for certification success with the Skillset library of over 100,000 practice test questions. We analyze your responses and can determine when you are ready to sit for the test. Along your journey to exam readiness, we will:

1. Determine which required skills your knowledge is sufficient
2. Which required skills you need to work on
3. Recommend specific skills to practice on next
4. Track your progress towards a certification exam