Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs(unit testing): improve unit testing guide #10183

Closed

Conversation

jackfranklin
Copy link
Contributor

This commit adds to the unit testing guide:

  • add explicit section on additional libraries: Karma, Jasmine and
    angular-mocks and link to the docs for those projects too. Explain the
    benefit and use case for each of these libaries
  • Add more fully featured test examples and add more documentation
    around them, in particular the controller test
  • more clearly distinguish between the section on principles of testing
    and actual tutorial on writing a test

This should close #8220.

Questions

One thing that I wasn't sure on was if to update the controller tests to not presume the controller is in the global scope. In the original issue (#8220) it says:

I think we talked about the controller syntax in #5076. We agreed to change that for 2.0.

So I left the controllers as is, but it feels beneficial to me to update the controller example. Is there any reason why that can't be done now? That issue mentioned (#5076) is more about the controllerAs syntax, but the main problem with the test example right now is that the controllers are global.

Secondly, the entire discussion about the different ways of dependency injection - most people will be familiar with how Angular does this and hence talking about other mechanisms doesn't seem that worthwhile?

@googlebot
Copy link

CLAs look good, thanks!

@jackfranklin jackfranklin changed the title docs(unit testing): improve unit testing Guide docs(unit testing): improve unit testing guide Nov 22, 2014
@jackfranklin jackfranklin force-pushed the unit-testing-docs-improving branch from 3d8aca7 to e28012c Compare November 22, 2014 19:52
```js
angular
.module('app')
.controller('PasswordCtrl', function PasswordCtrl($scope) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you rename this to PasswordController please? We are trying to get rid of the Ctrl suffix because it causes ambiguity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also use the DI annotations:

.controller('PasswordController', ['$scope', function PasswordCtrl($scope) { ...

@IgorMinar
Copy link
Contributor

we no longer support controllers in the global scope, so we should not use that in examples.

@btford can you also take a look please?

@jackfranklin jackfranklin force-pushed the unit-testing-docs-improving branch from e28012c to 474a629 Compare November 22, 2014 20:49
@jackfranklin
Copy link
Contributor Author

@IgorMinar so the dependency injection stuff kind of exists here: https://code.angularjs.org/snapshot/docs/guide/di

If we wanted to move the bit about DI that's in this guide, then the above link is probably the best place to put it.

@jackfranklin
Copy link
Contributor Author

we no longer support controllers in the global scope, so we should not use that in examples.

Cool, that's what I thought. I will update the examples :)

below:
## Additional libraries for testing Angular applications

For testing Angular applications there are libraries that you should use that will make testing much
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "tools" instead of "libraries." Karma isn't really a "library"

@jackfranklin jackfranklin force-pushed the unit-testing-docs-improving branch from 474a629 to 393e4ce Compare November 22, 2014 21:00
});
```

The duplication is now gone, and encapsulated within the `beforeEach`. Each individual test now only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplication is now gone, and encapsulated within the beforeEach

This reads weirdly to me. Consider something like "We moved duplicate behavior into the beforeEach block" instead.

@btford
Copy link
Contributor

btford commented Nov 22, 2014

@jackfranklin – when you're done could you squash these commits? Thanks!

@jackfranklin jackfranklin force-pushed the unit-testing-docs-improving branch from e7a4a1e to 0ce7034 Compare November 22, 2014 21:08
@jackfranklin
Copy link
Contributor Author

@btford how do you feel about moving the section on DI (https://github.com/jackfranklin/angular.js/blob/unit-testing-docs-improving/docs/content/guide/unit-testing.ngdoc#L38) out, and maybe putting it onto this page? https://code.angularjs.org/snapshot/docs/guide/di

It doesn't really make sense to exist here, better to just link off to the DI guide.

Edit: in fact, most of the content on DI that exists in this guide already exists in the DI guide, so maybe it's just a case of getting rid of it?

@btford btford self-assigned this Nov 22, 2014
@btford btford added this to the 1.3.5 milestone Nov 22, 2014
@btford
Copy link
Contributor

btford commented Nov 22, 2014

@jackfranklin – totally agree. Move that section to the DI docs, and if you can find a place in the testing docs, link to the moved section.

@btford
Copy link
Contributor

btford commented Nov 22, 2014

If the info is duplicated you can just drop it.

@jackfranklin
Copy link
Contributor Author

@btford can you take a look at the last commit? Once it's done I'll squash :)

@btford
Copy link
Contributor

btford commented Nov 22, 2014

Looks good. Squash away!

This commit adds to the unit testing guide:

- add explicit section on additional libraries: Karma, Jasmine and
  angular-mocks and link to the docs for those projects too. Explain the
  benefit and use case for each of these libaries
- Add more fully featured test examples and add more documentation
  around them, in particular the controller test
- more clearly distinguish between the section on principles of testing
  and actual tutorial on writing a test

This should close angular#8220.
@jackfranklin jackfranklin force-pushed the unit-testing-docs-improving branch from 0aa560e to 89179db Compare November 22, 2014 21:36
@jackfranklin
Copy link
Contributor Author

Woo! Squished :)

@btford
Copy link
Contributor

btford commented Nov 22, 2014

Landed as 3109342. Thanks for the awesome work!

@btford btford closed this Nov 22, 2014
@IgorMinar IgorMinar modified the milestones: ng-fixit #1, 1.3.5 Nov 22, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve unit testing docs in guide
4 participants