shows the code that implements figure 4.5. in Visual C#

Generating QR Code ISO/IEC18004 in Visual C# shows the code that implements figure 4.5.

Listing 4.2 shows the code that implements figure 4.5.
Paint QR-Code In C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
Reading QR Code ISO/IEC18004 In C#.NET
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Figure 4.5 The web service will be stubbed out to simulate an exception; then the email sender will be mocked to see if it was called correctly. The whole test will be about how LogAnalyzer interacts with other objects.
Barcode Generator In C#.NET
Using Barcode creator for .NET framework Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
EAN13 Maker In Visual C#
Using Barcode drawer for .NET framework Control to generate, create UPC - 13 image in .NET framework applications.
www.OnBarcode.com
Interaction testing using mock objects
Generating ANSI/AIM Code 128 In C#.NET
Using Barcode printer for .NET Control to generate, create Code 128 Code Set A image in .NET framework applications.
www.OnBarcode.com
Creating Data Matrix In C#.NET
Using Barcode drawer for .NET Control to generate, create Data Matrix ECC200 image in .NET framework applications.
www.OnBarcode.com
Listing 4.2 Testing the LogAnalyzer with a mock and a stub
ANSI/AIM Code 39 Creator In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create USS Code 39 image in .NET applications.
www.OnBarcode.com
Make Identcode In Visual C#.NET
Using Barcode generator for .NET Control to generate, create Identcode image in .NET framework applications.
www.OnBarcode.com
public class LogAnalyzer2 { private IWebService service; private IEmailService email; public IWebService Service { get { return service; } set { service = value; } } public IEmailService Email { get { return email; } set { email = value; } } public void Analyze(string fileName) { if(fileName.Length<8) { try { service.LogError("Filename too short:" + fileName); } catch (Exception e) { email.SendEmail("a","subject",e.Message); } } } } [TestFixture] public class LogAnalyzer2Tests { [Test] public void Analyze_WebServiceThrows_SendsEmail() { StubService stubService = new StubService();
Quick Response Code Creator In Visual Studio .NET
Using Barcode encoder for Reporting Service Control to generate, create Quick Response Code image in Reporting Service applications.
www.OnBarcode.com
QR-Code Decoder In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Using a mock and a stub together
Making QR Code In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code 2d barcode image in .NET applications.
www.OnBarcode.com
Print EAN13 In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create EAN-13 Supplement 5 image in ASP.NET applications.
www.OnBarcode.com
stubService.ToThrow=
EAN13 Maker In Objective-C
Using Barcode encoder for iPad Control to generate, create EAN-13 image in iPad applications.
www.OnBarcode.com
GS1-128 Creator In Java
Using Barcode maker for Android Control to generate, create GS1 128 image in Android applications.
www.OnBarcode.com
new Exception("fake exception");
Create PDF 417 In None
Using Barcode encoder for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
Generating USS-128 In .NET
Using Barcode printer for .NET Control to generate, create UCC.EAN - 128 image in .NET applications.
www.OnBarcode.com
MockEmailService mockEmail = new MockEmailService(); LogAnalyzer2 log = new LogAnalyzer2(); //we use setters instead of //constructor parameters for easier coding log.Service = stubService log.Email=mockEmail; Using properties instead of constructor injection string tooShortFileName="abc.ext"; log.Analyze(tooShortFileName); Assert.AreEqual("a",mockEmail.To); Assert.AreEqual("fake exception",mockEmail.Body); Assert.AreEqual("subject",mockEmail.Subject); } } public class StubService:IWebService { public Exception ToThrow; public void LogError(string message) { if(ToThrow!=null) { throw ToThrow; } } } public class MockEmailService:IEmailService { public string To; public string Subject; public string Body; public void SendEmail(string to, string subject, string body) { To = to;
Creating GS1 - 13 In Objective-C
Using Barcode maker for iPhone Control to generate, create EAN13 image in iPhone applications.
www.OnBarcode.com
Code 3/9 Maker In None
Using Barcode creation for Software Control to generate, create Code39 image in Software applications.
www.OnBarcode.com
Interaction testing using mock objects
PDF-417 2d Barcode Scanner In VB.NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Barcode Maker In None
Using Barcode encoder for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Subject = subject; Body = body; } }
Note that the public properties we ve added will be used instead of a constructor. We ll use these setters to set the stub and mock, which will make the code easier to read and maintain. This code raises some interesting questions: Why are we doing several asserts in a single test How easy would it be to separate this test into three different tests with one assert each Could the three asserts be combined into a single logical test It can be quite tedious to create manual mocks and stubs for each test or test class. How can we overcome that Couldn t we have used the MockService from listing 4.1 as a stub We ll explore answers to these questions in the rest of this and the next chapter.
One important thing to consider is how many mocks and stubs you can use in a test.
4.5 One mock per test
In a test where you test only one thing (which is how I recommend you write tests), there should be no more than one mock object. All other fake objects will act as stubs. Having more than one mock per test usually means you re testing more than one thing, and this can lead to complicated or brittle tests. (Look for more on this in chapter 7.) If you follow this guideline, when you get to more complicated tests, you can always ask yourself, Which one is my mock object Once you ve identified it, you can leave the others as stubs and not worry about assertions against them. Next, we ll deal with a more complex scenario: using a stub to return a stub or a mock that will be used by the application.
Stub chains: stubs that produce mocks or other stubs
4.6 Stub chains: stubs that produce mocks or other stubs
One of the most common scamming techniques online these days follows a simple path. A fake email is sent to a massive number of recipients. The fake email is from a fake bank or online service claiming that the potential customer needs to have a balance checked or to change some account details on the online site. All the links in the email point to a fake site. It looks exactly like the real thing, but its only purpose is to collect data from innocent customers of that business. This simple chain of lies is known as a phishing attack, and is more lucrative that you d imagine. Most people respond instinctively to these emails and do what s asked. Consequently, it s one of the biggest threats to identity theft in the world. How does this chain of lies matter to us Sometimes we want to have a fake component return another fake component, producing our own little chain of stubs in our tests, so that we can end up collecting some data during our test. A stub leads to a mock object that records data. The design of many systems under test allows for complex object chains to be created. It s not uncommon to find code like this:
IServiceFactory factory = GetServiceFactory(); IService service = factory.GetService();
Or like this:
String connstring = GlobalUtil.Configuration.DBConfiguration.ConnectionString;
Suppose you wanted to replace the connection string with one of your own during a test. You could set up the Configuration property of the GlobalUtil object to be a stub object. Then, you could set the DBConfiguration property on that object to be another stub object, and so on. It s a powerful technique, but you need to ask yourself whether it might not be better to refactor your code to do something like this:
Copyright © OnBarcode.com . All rights reserved.