I'm currently working on a legacy application which uses ASP.Net web forms. Pages in this application have a mixture of formatting and logic in the aspx and ascx files. Typically things like:
if (number of passengers > 1)
include singlePassengerForm.ascx
else
include multiPassengerForm.ascx
but think a lot more complicated - nested ascx includes, lots more logic, much bigger pages, etc.
I would like to fix this, but slowly. The final design I'm currently driving towards is a very light page/control which won't have any logic, but would just pick things from a model (including which ascx to embed). I like the fact that the pages and controls are broken down into smaller pieces and I'd like to keep that. So this would translate to an interconnected model (eg: ReservationFormModel has a PassengerFormModel - which could be a SinglePassengerForm or a MultiplePassengerForm) and a set of interconnected pages with probably a set of controllers to populate the model and connect the model and the page. So sort of MVCish. Basic idea being, I want to unit test that logic.
The other thing I want to do is fix this as an on going process - not in one big bang. So I'd like to be able to start small, where I fix just one part of a page while the rest of the page still uses the old way of embedded logic and data in the aspx/ascx.
Any ideas/suggestions/experience reports around this would be really appreciated.
I've seen the answers to this: What is the best way to do unit testing for ASP.NET 2.0 web pages? and this: Unit Testing Legacy ASP.NET Webforms Applications
but I'm looking for something more specific
As you know WebForm path is not the most testable way to write code. A key aspect of MVC pattern is in fact that controllers can be tested much better.
A simple solution is to include core logic in "Logic" classes like MVC Controllers and use page behind code only to bind data to controls. You can then use, for example, NUnit framework to create test fixture and NCover to monitor test code covering.
Related
I would like to create functional tests to cover my Django project. It is a multipage form that accepts input on each page. The input on previous pages changes the content/options for the current pages. The testing is currently set up with Splinter and PhantomJS. I see two main ways to set this up.
For each page, create an instance of that page using stored data and point Splinter towards that.
Benefits
Allows random access testing of any page in the app
Can reuse unit test definitions to populate these synthetic pages
Downsides
Needs some kind of back end to be set up that Splinter can point to (At this point I have no idea how this would work, but it seems time consuming)
Structure the testing so that it is done in order, with the test content of page 1 passed to page 2
Benefits
Seems like it should just work out of the box
Downsides
Does not allow the tests to be run in an arbitrary order/only one at a time
Would probably take longer to run
Errors on earlier pages will affect later pages
I've found many tutorials on how to do functional testing on a small scale (individual pages/features/etc.) but I'm trying to figure out if there is an accepted way or best practice on how to structure it over a large project. Is it one of these? Something else that I've missed?
What I was looking for was fixtures (https://docs.djangoproject.com/en/1.11/ref/django-admin/#django-admin-dumpdata). Things just get way too complex if you're trying to pass browser state between a whole project worth of tests. Easy to grab the DB state, easy to load in.
I'd like to create a highly maintainable code, with unit tests.
I have read the best practices so directories are organized as per best practice, with just one bundle named AppBundle, and I'm using annotations.
I have an issue with business logic. I've read that I should not put business logic inside controllers. As the vast majority of my code was not meant to be reused I put the logic inside the controllers. Yesterday I read here that "logic should live in the controller unless you’re going to unit test it or until you need to re-use it" and this made me faint: am I not going to unit test if I put my logic inside controllers?
I understand that "controllers should be as lean as possible, with few lines of code to glue stuff together". But what about forms? A form often has a logic like "display the form, if it's valid do something and display another page". Now if I'm going to put the form creation and logic inside a service (or a model?) I'd have to put the page render command inside that, so basically the controller would be 1 line, with all the rest inside the service, and the actual decision of which page to display would be done inside the service, not the controller itself...so what's the point of a controller, just the routing?
I really need to understand this before proceeding (i've already developed 3 months on this and I'd have to rework a lot, but better now than never)...
Thank you!
EDIT: some additional considerations to address some of the comments below.
I have a clear understanding of how forms work in Symfony, with the creation of the form and it's management with the "isValid()" in the same place.
Let's imagine the following controller which performs the following operations:
get the current customer from security context
queries the DB to get the field $user->getIsBillable()
if the user is not billable
queries the DB to find if someone else is paying for him
else //the user is billable
create and manage form1
if the user is not billable but there is someone who is paying for him
check if the license is active (I have to call an external API)
if the license is not ok
redirect to a page to update credit card info
if the user is not billable
create and manage form2
else
create and manage form3
render the license management page with all the necessary forms created
What would be supposed to do? Put all this stuff into a service which would return the forms (or null if a form is not created)? Consider that in the "is valid" of some of the forms I render the same page with simply updated info. Or should I create a service foreach of the forms creation and keep the logic in the controller?
You should keep your logic outside the controllers in order for your code to be more reusable and to reduce code duplication.
The best practice is to have your business logic registered as a Symfony service instead.
The controller should be as lean as possible as it only handles the view and accessing your services to run business logic functionality.
Using services will make your code more reusable and it will be easier to write unit tests for each part of your code.
Forms are handled in a different way in Symfony2 and you should read more about it: http://symfony.com/doc/current/book/forms.html
Like most php frameworks, symfony2 has its problems.
Turns out symfony2 best practices are not always best practices. But in most cases symfony allows for loose coupling and proper dependency injection, it just doesn't tell you in the "book".
For example a very important topic: http://symfony.com/doc/current/cookbook/controller/service.html
I usually have my form types as services, my controllers as services and other services for the business logic. My controller actions are ideally around 3-5 lines of code. Sometimes when handling a file upload they will be slightly longer though.
You can unit test your business logic services, but for your controllers and forms you will need some sort of integration testing, or use heavy mocking.
I understand that "Selenium" is a good tool for testing such things, I haven't used it yet.
One thing that I would like add to other answers provided there to you is that Unit Test is different from Functional Test.
Unit Test
You need Unit Test when you want to test individual "unit" of code (such as method in a class)
Functional Test
You need Functional Test when you want to test a "suite" of functionalities (like many methods interactions, db interactions, web services and so on)
Recap
Ideally you want to write Unit Test(s) for a class that contains heavy business logic whereas you need Functional Test(s) for testing whole controller (so a page is render with all class interactions and db ones, the page is render, the page is posted and so on)
The best approach, to me - and it's only an opinion of a devel that tried as today to understand testing from a newbie point of view - is to mix each other: with Unit Test (and isolation, of course!) I'm sure that (simplifying) every input that I provide to my method will return the correct output, while functional test will help me to test the whole thing and interactions.
Answer
So, returning to your question - that seems wider that at first look could seems - a good way to procede is to keep all business logic outside controllers (even if you're sure [and to me you'll never be sure enaugh] that code you put in will be never reused elsewhere) by defining services (as said in other answers). Then you will do some unit test(s) onto your services and after that you will probably need some Functional Test(s) to check that all "the glue keep things together correctly".
I don't know if I'm missing something (it's likely). Just to be more complete, I will attach my older question on Symfony2 (I was studying it and MVC pattern at the time)
Structuring your code to be clean and maintainable it is a very hard stuff, I suggest you to start studying the SOLID principles than having a superficial understanding of MVC:
Single Responsability Principle - SRP
Open/Closed Principle - OCP
Liskov Substitution - LSP
Interface Segregation - ISP
Dependency Inversion Principle - DIP
Hi one of my client needs unit tested application in asp .net webforms. But I have no Idea about how to start or unit tested applicaton/MVC. I would like to know how to create unit tested application in webforms. Any example, video, tutorial will be helpfull.
Thnaks
I suggest you do the following things:
Start asking your client and yourself what it is you want to achieve? Merely unit testing for no other reason than saying you are unit testing is not a worthy goal to pursue.
Read some introductory book like The Art Of Unit Testing to make yourself familiar with unit testing.
Consider reading this ASP.NET MVC book because it takes you through all layers of an ASP.NET MVC app while applying unit testing at each of those layers (with really good examples).
I agree with point 1 of meilke's answer. Adding unit testing to an existing application can be a lot of work and without a good reason, it's unlikely a customer will want to pay for it. It is possible however to use a request to add or remove functionality as an opportunity to get started. For example, if you have a task to remove some existing functionality then putting a set of unit tests in place to verify the existing behaviour of the application can save headaches later on. Think of it like putting scaffolding in place before doing construction work.
It's not clear from your question but if it's ASP.NET WebForms as distinct from ASP.NET MVC that you are looking to unit test, I would recommend looking into the Model View Presenter design pattern. Here and here are a couple of articles that should help.
I need to take the results of a web service and display them in a front-end component view. I am returning the results and they are in JSON format but am not sure how the component should be built. I don't need a debate on the best way, just some advice on a good way to built this as a component i will be able to install on multiple Joomla 3 instances.
Once thing that would help greatly is some direction on where to place the code that consumes the web service. Would that be a component model? Or better as a library sitting outside the component?
Thanks
Seems like you need some fast solution before learning the MVC architecture in Joomla!3 so probably a good suggestion would be to build a sample component at http://www.component-creator.com/home and then work and learn from there.
Then for the code for the webservices, it depends on what you want to do with the code, but if it's just to render in the view as you get it, use the view.html.php in your component views directory. Then, in your tmpl default view use the data to render as required.
Further you can create your own methods on the models or controllers in case you need to do something with the data, but before that, try to understand the Joomla! 3 main MVC architecture principles ;)
After learning about TDD and unit testing, I'm really looking to write tests when I code, preferably before I code because I see the benefits coding in Python. I'm developing a website, and I'm trying to write tests according to the requirements, but its proving more difficult than expected.
I can see the benefits of writing tests when you're producing a library of code with a public interface for others to use. Developing a website, where there is really not much logic, and mostly Reading and Writing against a database seems a little harder to unit test. Mostly, I have to create/edit/delete rows in the database.
I'm using a framework (Kohana 3 for php), so 99% of all the libraries and helpers that I'm going to be using have already been tested (hopefully), so what else is their to write tests for?
I'm mostly talking about scripting languages, not about CSS or HTML, I already know about cross-browser testing.
How much can you really test when developing a web site, and how should you go about it?
Edit: Is the lack of activity on this question a sign? I understand that certain things MUST be tested, like security and the like, but how much can be written using unit tests and TDD is the question.
Thanks.
Developing a website, where there is really not much logic, and mostly Reading and Writing against a database seems a little harder to unit test. Mostly, I have to create/edit/delete rows in the database.
Not completely true.
You have data model processing. Does the validation work? Do the calculations on the reported rows from the database work?
You have control, sequence and navigation among pages -- do the links work? The test setup will provide a logged-in-user. The test will (1) do a GET or a POST to fetch a page, then (2) confirm the page actually loaded and has the right stuff.
You have authorization -- who can do what? Each distinct test setup will provide a different logged-in-user. The tests will (1) attempt a GET or POST to process a page. Some tests will (2) confirm they got directed to error-response pages. Some tests will (2) confitrm that the GET or POST worked.
You have content on the page -- what data was fetched? The test setup will provide a logged-in-user. The test will (1) do a GET or a POST to fetch a page, then (2) confirm the page actually loaded and has the right stuff.
Have you tried Selenium? It allows you to automatically do almost anything in a web browser. For example, you could have it go through and click all of the links and make sure that they go to the correct url.
It works with multiple languages, including python and allows for testing in chrome, firefox, ie, and other browsers.
If your site contains many forms, how do you write them? Do you write each view using plain HTML? Or do you write your own form helpers that generate forms just the way you want them? If you do that, you may find that unit-testing your form generators makes it easier to write them.
In general, if your program is mostly CRUD, look out for ways to automate CRUD management; write your own custom CRUD generator. Which does not mean write the CRUD framework that will end all frameworks; that would be too much work. Just write a generator for the small things you need for your current application. TDD will help you there.