One of the other issue you might encounter while unit testing your Controller is when you dealing with Identity. Consider the following action method. public async Task<BarResponse> Foo(BarRequest user) { if (ModelState.IsValid) { try { var userName = User.Identity.Name; // Do Task return new BarResponse{ }; } catch (Exception ex) { return new BarResponse { … Continue reading Mocking User.Identity.Name
Category: Quality Focus
Writing Unit Test for Model Validation
It is a pretty common pattern to validate your Web Action model using ModelState.IsValid method. For example, public async Task<UpdateUserProfileResponse> UpdateUser(UpdateUserProfileRequest user) { if (ModelState.IsValid) { // Valid Model, do your job } else { // Send response indicating invalid model } } A pretty useful pattern, as it makes use of the DataAnnotations to validate and provide meaning … Continue reading Writing Unit Test for Model Validation
3 Rules for TDD
I couldn't help but share this. There couldn't be a better way to summarize the unwritten rules of TDD, than the one wrote by Uncle Bob in his famous book 'Agile Principles, Patterns and Practices in C#''. Don't write any production code until you have written a failing unit test. Don't write more of a … Continue reading 3 Rules for TDD
Mock RestClient.ExecuteAsync
RestSharp is not a stranger to developers, so is the usage. What this posts aims to explore is how to do Unit Test a method, involving a RestClient.ExecuteAsync call. To be more precise, the post explores how to mock the RestClient for the method. Let's consider the following code, taken from VSPostman, one of the … Continue reading Mock RestClient.ExecuteAsync
Zero Bug Bounce (ZBB)
ZBB or Zero Bug Bounce is a defect management technique originating from Microsoft that represents the moment (would prefer to call it a milestone) in time when the developers has managed to fix (or triaged) all the bugs reported by the QA team. The bounce refers to the fact the QA team might (and surely … Continue reading Zero Bug Bounce (ZBB)
Unit Testing Asynchronous Code
Unit Testing Asynchronous methods in older days were pretty tough, but thanks to the advances made in the Unit Testing Frameworks, this is now all the more easier. We will begin by writing an async method, which we could use for writing our Unit Tests against. The advancements in Unit Testing Framework has made it … Continue reading Unit Testing Asynchronous Code
Mutation Testing
Even with TDD, the quality or coverage of the existing Unit Tests are always a concern for the developer. "Have I done enough ?" That's one question that haunts him throughout. Mutation Testing is a structural testing method that attempts to add more value to Unit Tests by aiding in identifying the 'misses' in Unit … Continue reading Mutation Testing
Unit Test for Methods that Call Web API
HttpClient helps you interact with your Rest API's through its asynchronous methods such as GetAsync. But on a TDD environment, it could become a challenge on how you could write Unit Test cases for a method that invokes the API via GetAsync and ensure that your call successfully invokes the API (considering your API sits in another … Continue reading Unit Test for Methods that Call Web API