I know that the reason that Microsoft came out with ASP.NET MVC was to make it simpler to do Test Driven Design (TDD) for ASP.NET. However, I have a rather large brown field (existing) application in ASP.NET WebForms that I would love to implement some TDD type functionality in. I'm assuming that there IS a way to do this, but what are some viable options?
TDD means writing tests to implement a requirement and continuously iterate through RED GREEN and REFACTOR cycles. Advantages. TDD makes you think with the needed API from the beginning. You need to think about what classes, properties, API's are needed. This will usually lead to great API design.
One of the greatest advantages of ASP.NET MVC is the support of testability, which enables to Test-Driven Development (TDD) in an easy manner.
ASP.NET Web Forms isn't supported in ASP.NET Core (nor are ASP.NET Web Pages). Typically, the functionality of these pages must be rewritten when porting to ASP.NET Core. There are, however, some strategies you can apply before or during such migration to help reduce the overall effort required.
Microsoft introduced ASP.NET MVC because they thought they could make money from an untapped market - those who feel that Web Forms are too "heavyweight", and who are programming using a lighter-weight framework. This includes those who are accustomed to the MVC paradigm.
It also includes those who couldn't figure out how to do unit tests in web forms, and who want to use unit tests and TDD.
The way to do it with web forms, as with anything else, is to separate everything but the UI code into separate classes in a class library. Use TDD to develop those classes.
The next layer of controversy is whether it's necessary to use TDD to develop the remainder of the code: the markup, client-side code, user interactions, etc. My answer is that if you've got the rest isolated and tested, that it's not worth the trouble to use TDD for this.
Consider: your pages need to have a particular appearance. Are you going to write a failing unit test to prove that you are using CSS correctly? To prove that you're using the correct CSS styles? I don't think so.
To clarify: In TDD, we start with a failing unit test. We then make the simplest possible changes that will make the test succeed.
Imagine using TDD for a web page. What failing tests will you produce?
And none of the above tests for the appearance of the page. None of it tests the client-side behavior of any JavaScript on the page.
I think that's silly. Instead, test your DAL method that retrieves data based on the ID. Make sure it returns the correct ID for id 1. Then, how long will it take to manually test the page to make sure it looks correct, you can enter the "1" and click "Go", and that the data that appears in the grid is the correct data for customer 1?
Test-Driven Development and automated unit tests are meant to test behavior. The UI of a web form is mostly declarative. There's a large "impedance mismatch" here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With