O'Reilly logo

The Art of Unit Testing, 2nd Edition by Roy Osherove

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.4. Using a mock and a stub together

Let’s consider a more elaborate problem. This time LogAnalyzer not only needs to talk to a web service, but if the web service throws an error, LogAnalyzer has to log the error to a different external dependency, sending it by email to the web service administrator, as shown in figure 4.4.

Figure 4.4. LogAnalyzer has two external dependencies: web service and email service. You need to test LogAnalyzer's logic when calling them.

Here’s the logic you need to test inside LogAnalyzer:

if(fileName.Length<8) { try { service.LogError("Filename too short:" + fileName); } catch (Exception e) { email.SendEmail("a","subject",e.Message); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required