Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Pragmatic Test-Driven Development in C# and .NET

You're reading from  Pragmatic Test-Driven Development in C# and .NET

Product type Book
Published in Sep 2022
Publisher Packt
ISBN-13 9781803230191
Pages 372 pages
Edition 1st Edition
Languages
Author (1):
Adam Tibi Adam Tibi
Profile icon Adam Tibi

Table of Contents (21) Chapters

Preface 1. Part 1: Getting Started and the Basics of TDD
2. Chapter 1: Writing Your First TDD Implementation 3. Chapter 2: Understanding Dependency Injection by Example 4. Chapter 3: Getting Started with Unit Testing 5. Chapter 4: Real Unit Testing with Test Doubles 6. Chapter 5: Test-Driven Development Explained 7. Chapter 6: The FIRSTHAND Guidelines of TDD 8. Part 2: Building an Application with TDD
9. Chapter 7: A Pragmatic View of Domain-Driven Design 10. Chapter 8: Designing an Appointment Booking App 11. Chapter 9: Building an Appointment Booking App with Entity Framework and Relational DB 12. Chapter 10: Building an App with Repositories and Document DB 13. Part 3: Applying TDD to Your Projects
14. Chapter 11: Implementing Continuous Integration with GitHub Actions 15. Chapter 12: Dealing with Brownfield Projects 16. Chapter 13: The Intricacies of Rolling Out TDD 17. Index 18. Other Books You May Enjoy Appendix 1: Commonly Used Libraries with Unit Tests 1. Appendix 2: Advanced Mocking Scenarios

Appendix 2: Advanced Mocking Scenarios

This book has numerous examples of straightforward mocking scenarios. And the good news is, in a clean code environment, implementing most of those mocking requirements will be easy.

However, there are times when you have to innovate a bit to be able to mock your desired class. I did not want to end this book without presenting you with a scenario, so here you go.

In this appendix, we will experience how to combine a fake with a mock to deal with a .NET class called HttpMessageHandler. By the end of this appendix, you will be familiar with more NSubstitute functionalities and ready to tackle more advanced mocking cases.

Technical requirements

The code for this chapter can be found at the following GitHub repository:

https://github.com/PacktPublishing/Pragmatic-Test-Driven-Development-in-C-Sharp-and-.NET/tree/main/appendix2

Writing an OpenWeather client library

We have used the OpenWeather service in this book frequently, so I will give you a quick reminder – OpenWeather provides a set of RESTful APIs to bring you the weather and the forecast.

To consume the service from a C# application, it is good to have a library that will translate RESTful API calls to C# and shield the developer from the little details of HTTP. We call this sort of library a RESTful client library or sometimes a software development kit (SDK).

We will build an SDK for this service using TDD (of course!) and, while doing so, we will encounter more advanced mocking requirements.

The One Call API

OpenWeather has an API called One Call that will get you today’s weather information and the forecast for the next few days. The best way to illustrate how this works is by using an example that gets the weather and the forecast for Greenwich, London.

First, issue a RESTful GET request. You can use your browser...

Further reading

To learn more about the topics discussed in the chapter, you can refer to OpenWeather’s official website: https://openweathermap.org

lock icon The rest of the chapter is locked
arrow left Previous Chapter
You have been reading a chapter from
Pragmatic Test-Driven Development in C# and .NET
Published in: Sep 2022 Publisher: Packt ISBN-13: 9781803230191
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}