AngularJS / Karma testing: beforeEach not executed
The reason your log message console.log("in beforeEach...");
is not being displayed is because it is not actually inside beforeEach
, it is inside an anonymous function passed to a module(..)
as an argument which is considered to be a module by angular-mocks. This module will be executed only when injection happens and at the same time you'll receive a log message in beforeEach...
, but there is no any injection in your test, so it never happens. beforeEach
fires anyway, you just didn't put console.log
in the right place; it will work:
beforeEach(function () { console.log("in beforeEach..."); module('userApp', function($provide) { // Output messages $provide.value('$log', console); });});
Also it seems that you forgot to inject mocked $log
into you test suite, your $log
variable never gets any value, so it stays undefined as the error states.
describe('userApp', function(){ var $log; beforeEach(function () { console.log("in beforeEach..."); module('userApp', function($provide) { // Output messages $provide.value('$log', console); }); // getting an instance of mocked service to use in a test suite inject(function (_$log_) { $log = _$log_; }); }); it('should be able to log something', function(){ console.log("in it..."); $log.info("Using $log for logging..."); }); });
See the plunker: http://plnkr.co/edit/EirNEthh4CXdBSDAeqOE?p=preview
Docs: