Interaction testing using mock objects in C#

Encode QR Code ISO/IEC18004 in C# Interaction testing using mock objects

Interaction testing using mock objects
Generating QR Code 2d Barcode In Visual C#
Using Barcode creator for .NET Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
QR Scanner In C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
This chapter covers
Paint Code 3 Of 9 In Visual C#
Using Barcode creator for .NET framework Control to generate, create Code 39 image in VS .NET applications.
www.OnBarcode.com
Encoding Matrix In Visual C#
Using Barcode drawer for .NET Control to generate, create Matrix Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Defining interaction testing Understanding mock objects Differentiating mocks and stubs Exploring mock object best practices
Barcode Drawer In C#
Using Barcode maker for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Painting QR Code JIS X 0510 In C#
Using Barcode encoder for .NET framework Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
n the previous chapter, we solved the problem of testing code that depends on other objects to run correctly. We used stubs to make sure that the code under test received all the inputs it needed so that we could test its logic independently. In this chapter, we ll look at how you test whether an object calls other objects correctly. The object being called may not return any result or save any state, but it has complex logic that needs to result in correct calls to other objects. Using the approach we ve employed so far won t do here, because there s no externalized API that we can use to check if something has changed in the object under test. How do you test that your object interacts with other objects correctly We ll use mock objects. The first thing we need to do is define what interaction testing is, and how it s different from the testing we ve done so far state-based testing.
PDF 417 Encoder In Visual C#.NET
Using Barcode generator for .NET framework Control to generate, create PDF417 image in Visual Studio .NET applications.
www.OnBarcode.com
Generating MSI Plessey In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create MSI Plessey image in Visual Studio .NET applications.
www.OnBarcode.com
State-based versus interaction testing
Painting QR Code 2d Barcode In Objective-C
Using Barcode printer for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
QR Code Encoder In None
Using Barcode drawer for Online Control to generate, create Quick Response Code image in Online applications.
www.OnBarcode.com
4.1 State-based versus interaction testing
Paint GS1-128 In None
Using Barcode encoder for Microsoft Excel Control to generate, create UCC.EAN - 128 image in Excel applications.
www.OnBarcode.com
Read EAN 128 In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
We defined state-based testing in section 2.6 of chapter 2. Let s define interaction testing, and then look at how we use it in our unit tests.
Drawing Matrix 2D Barcode In Java
Using Barcode generation for Java Control to generate, create Matrix image in Java applications.
www.OnBarcode.com
Making GS1 - 13 In None
Using Barcode creator for Online Control to generate, create EAN-13 image in Online applications.
www.OnBarcode.com
DEFINITION Interaction testing is testing how an object sends input to or receives input from other objects how that object interacts with other objects.
Data Matrix 2d Barcode Maker In None
Using Barcode drawer for Online Control to generate, create ECC200 image in Online applications.
www.OnBarcode.com
Read Barcode In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
You can also think of interaction testing as being action-driven testing, and state-based testing as being result-driven testing. Actiondriven means that you test a particular action an object takes (such as sending a message to another object). Result-driven means you test that some end result is now true (that a property value has changed, for example). It s usually preferable to check the end results of objects, not their particular actions. But sometimes interactions between objects are the end result. That s when we need to test the interaction itself (where the end result of calling a method on the object under test is that the object then calls another object, such as a web service). Interaction testing, in one form or another, has existed since the first days of unit testing. Back then, there weren t any names or patterns for it, but people still needed to know if one object called another object correctly. Let s look at an example of the two types of testing. Say you have a watering system, and you have given your system specific instructions on when to water the tree in your yard: how many times a day and what quantity of water each time. Here s how you d test that it s working correctly:
Code 128 Code Set B Creation In None
Using Barcode drawer for Microsoft Word Control to generate, create Code 128A image in Microsoft Word applications.
www.OnBarcode.com
Data Matrix ECC200 Maker In None
Using Barcode creation for Office Word Control to generate, create Data Matrix ECC200 image in Microsoft Word applications.
www.OnBarcode.com
State-based testing Run the system for a specific amount of time (say 12
Making QR Code 2d Barcode In None
Using Barcode drawer for Software Control to generate, create Quick Response Code image in Software applications.
www.OnBarcode.com
Read Data Matrix 2d Barcode In Visual Basic .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
hours), and at the end of that time, check the state of the tree being irrigated. Is the land moist enough, is the tree doing well, are its leaves green, and so on. It may be quite a difficult test to perform, but assuming you can do it, you can find out if your watering system works. Interaction testing At the end of the irrigation hose, set up a device that records when irrigation starts and stops, and how much water flows through the device. At the end of the day, check that the device
Interaction testing using mock objects
has been called the right number of times, with the correct quantity of water each time, and don t worry about checking the tree. In fact, you don t even need a tree to check that the system works. You can go further and modify the system clock on the irrigation unit, so that it thinks that the time to irrigate has arrived, and it will irrigate whenever you choose. That way, you don t have to wait (for 12 hours in this example) to find out whether it works. Sometimes state-based testing is the best way to go because interaction testing is too difficult to pull off. That s the case with crash test dummies: a car is crashed into a standing target at a specific speed, and after the crash, both car and dummies states are checked to determine the outcomes. Running this sort of test as an interaction test in a lab can be too complicated, and a real-world state-based test is called for. (People are working on simulating crashes with computers, but it s still not close to testing the real thing.) Now, back to the irrigation system. What is that device that records the irrigation information It s a fake water hose, or a stub, you could say. But it s a smarter breed of stub a stub that records the calls made to it. That s partly what a mock object is.
Copyright © OnBarcode.com . All rights reserved.