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

It's not obvious that tests with promises must call $rootScope.$apply() #1915

Closed
inklesspen opened this issue Jan 29, 2013 · 8 comments
Closed

Comments

@inklesspen
Copy link

I just spent a few hours pulling my hair out because my tests weren't passing and I couldn't figure out why. It turns out it's because $q's promises don't actually get resolved during tests unless you call $rootScope.$apply. (This appears to have been masked in my earlier testing because I was testing services that used $timeout and $httpBackend and their flush methods.)

Please make sure to document this behavior when you improve the testing documentation.

@pkozlowski-opensource
Copy link
Member

@inklesspen, actually it is in the example test on this page: http://docs.angularjs.org/api/ng.$q

Would be willing sending a pull request with the proposal of additional explanations you would like to see?

@inklesspen
Copy link
Author

Yeah, and that's where I finally found it; I'd been expecting to find it in documentation such as http://docs.angularjs.org/guide/dev_guide.unit-testing or http://docs.angularjs.org/guide/dev_guide.services.testing_services

@pkozlowski-opensource
Copy link
Member

@inklesspen, going to close this for now since it is documented. Would be awesome if you could send a pull request with proposed improvements.

@inklesspen
Copy link
Author

So requests to improve the documentation are unwelcome unless I give you a pull request? Good to know.

@pkozlowski-opensource
Copy link
Member

@inklesspen they are highly welcomed! It is just that it is documented already but not in the place you were expecting it... so suggested to send a PR with your proposal. Not sure how to action on this one otherwise...

@kevin1024
Copy link

👍, I just burned a few hours banging my head against this exact problem until I found this github issue. The reason it's so confusing is that I'm not doing anything with $scope in my code or in my tests at all. I thought that I would only have to call $rootScope.$apply() if I wanted the scope to update.

@uriva
Copy link

uriva commented Dec 20, 2015

+1

@maennchen
Copy link

👍 also burned a few hours searching for the solution.

Narretz added a commit to Narretz/angular.js that referenced this issue Feb 8, 2016
Narretz added a commit to Narretz/angular.js that referenced this issue Feb 8, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants