Interested in a private company training? Request it here.
Every developer aspires to write code that is not only clean but also functional. However, achieving code that is easily maintainable and consistently operational poses a significant challenge. How does one navigate the complexities of software development to ensure that the code remains robust and reliable in the long run? In this context, we will explore the role of unit testing in verifying the correctness of our code and ensuring its continued functionality..
When it comes to Unit Testing, you will need to choose frameworks. There are several options available, so which one do you need? Visual Studio comes with a built-in framework called MSTest, but you also have others, such as the popular xUnit. In this module, we will discuss the pros and cons of these frameworks and delve into some best practices. How can you determine if you have tested enough? Code coverage allows you to see which paths in your code you have tested and easily identify untested conditional logic.
When writing code, it's crucial to minimize unnecessary dependencies on other objects. Dependencies can increase maintenance overhead, reduce testability, and limit flexibility. If you must rely on another object, consider depending on an interface. Interfaces describe the dependency without enforcing specific implementations. However, you should avoid hardcoding these dependencies. This is where dependency injection (DI) comes in handy. Dependency injection allows you to dynamically determine which implementations to inject into dependencies at runtime, providing greater flexibility and decoupling within your codebase.
If you aim to make your code testable, you must design it with testing in mind. In this course, you'll discover the concepts of stubs and mocks, and you'll master techniques for substituting dependencies that could disrupt your testing process.
Building stubs and mocks can be laborious without an Isolation Framework. NSubstitute is one such framework that simplifies the creation of fake and stub objects required for robust testing. But what about testing "untestable" code? Code where dependencies are tightly coupled to the class under test? For such scenarios, you need a bit of magic: Microsoft Fakes. This tool enables you to substitute any class's implementation, easing the testing of legacy and otherwise challenging-to-test code.
When creating modern web applications with ASP.NET Core MVC, unit testing the Controllers and Views isn't straightforward. How does one validate whether the ActionResult returned by the Controller action is the expected one, and whether the View is correct? How do we replace dependencies during testing with MVC? How do we perform end-to-end testing?
So your software works! Great! But how do you know if it will work next week, or next month? Did you break something while adding a new cool feature? Test everything again? Of course, but now we will automate it, making a machine do it automatically every time a team member makes a change.
Writing solid, maintainable code that works is the goal of every developer. This course guides you into the art of unit testing, where you learn to build testable code and various techniques to ensure its quality. By the end of this course, you will gain a solid and practical understanding of unit testing and its real-life applications. U2U is renowned for its hands-on approach to training, and each chapter is accompanied by a practical lab.
This course is intended for experienced programmers who are very familiar with C# and have a working experience with .NET 6/8. All examples and labs utilize the latest LTS version of .NET and Visual Studio.