$ compile vs $ componentController

I am relatively new to Angular and started writing my application using component architecture. When looking at the Angular docs, he recommends testing the components with the $componentController

mock method .

However, when I look at how directives have traditionally been tested in Angular pre-1.5, it looks like the preferred method was to use a service $compile

to actually build your template-template-template. By using $compile

, you can make assertions about your template logic as well as your controller logic. Whereas with the method $componentController

you can test controller logic that doesn't really seem to be that useful since most of the complexity is found in templates and services.

Can someone shed some light on current best practices? It makes sense for me to use $compile

so you can test the template as well. But why don't the Angular docs mention this at all and recommend it instead $componentController

?

+3


source to share


1 answer


One of the biggest problems with AngularJS is that it has $scope

. This is where you put your bindings that are used in the DOM. He introduced a lot of confusion.

Every good application design should have layers: business logic, user interface, etc. In AngularJS, these layers are pretty much the same controller

for business logic and directives

UI. However, due to what $scope

is available in directives

, many people have decided not to use controllers and put all the business logic in directives. This led to tough testing as they implemented both levels at the same time. Also, the test became slow as DOM manipulation is slow.



Ideally, you should test as accurately as possible in business logic and less testing in the user interface. Since the framework handles synchronization between the business logic and the UI, there is little room for error. But business logic is where most errors are introduced. Therefore, in new AngularJS they recommend using it $componentController

to validate business logic in controllers, not directives.

New Angular does not $compile

, and most tests are written for controllers that are implemented there as classes.

+3


source







All Articles