0

Given the official tutorial of angularJS,

https://docs.angularjs.org/tutorial/step_07#testing

It is apparent that it only tests the controller's states (e.g. its model) when events happen (e.g. http server response). However, this kind of tests cannot tests if the HTML correctly calls the appropriate methods of the controller.

On the contrary, if end-to-end testing (i.e. using protractor) can even tests the HTML correctly calling the appropriate methods, aside from testing what the unit tests of angularJS controller test.

Is there still a good reason why unit testing of angularJS controller should still be written?

1

This is not specific to angular, but a general concept known as the test pyramid

You are right that e2e tests are better because they function more like a actual user. It is easy to make unit tests that "prove" that the code works perfectly, but in reality it could be misconfigured making it completely broken.

As we can see in the test pyramic e2e tests have a higher cost. They also tend to be more fragile because they use a real database which is shared state. Think in a ecommerce type application one of the tests could have purchased all the inventory making the other tests fail.

It is also hard to test failure scenarious in an e2e test. This is much easier to test in other pyramid layers.

A problem with e2e is that a failing test could cover more than one root cause making it unpredictable how long time it takes to fix a broken system.

All in all a good mix is the best strategy.

  • I wouldn't save e2e tests are "better;" they are different, with different costs/benefits (as you point out). – Andy May 2 '17 at 22:52
  • "They also tend to be more fragile because they use a real database which is shared state." - isn't test decoupling possible by running a transaction scope in each test? – Xegara May 3 '17 at 2:49
  • @Xegara since e2e tests are supposed to use a real system and a real web server I don't see how you could expose something like transaction scope through all those layers. – Esben Skov Pedersen May 3 '17 at 5:18
  • I see. I don't see it as impossible, although it may be tricky to implement. All in all, do you recommend to write unit, integration and e2e tests even if their test coverages overlap with each other (i.e. redundant tests)? My initial notion of writing tests is that if a test covers an area of logic, then there's no need to write another layer of test to cover the same test. But I'm starting to understand that they are meant to complement each other. So redundant tests is not a problem. – Xegara May 3 '17 at 6:13
  • For instance, E2e tests are written to ensure that all user stories are working. If an e2e test fails, then it tells us there's a problem but it does not tell us where exactly the problem is. This is where the lower layers of the test pyramid come in. Integration tests test smaller regions of code than e2e test but bigger regions of code than unit test. It narrows it down where subregion is the problem until we reach the unit test. Am I right? – Xegara May 3 '17 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.