5

Can you please explain how this line works:

beforeEach(module('phonecatApp'));

beforeEach() expects a callback function to call before each test. module() returns an angular.Module object.

What does beforeEach() do with an object?

Laszlo B
  • 455
  • 3
  • 14

1 Answers1

3

If you look in the source of angular.mock.module you can see it either returns a function, or the result of a function, depending on whether a spec is running:

window.module = angular.mock.module = function() {
  var moduleFns = Array.prototype.slice.call(arguments, 0);
  return isSpecRunning() ? workFn() : workFn;
  /////////////////////
  function workFn() {
    ...

When beforeEach is called, I suspect this is treated as not being during a spec, so the function returns a callback that runs when the test runner later calls the callbacks registered with beforeEach.


Also I don't see documented, or in the source, that it actually returns a module object. It apparently registers a module with the dependency injection system.

Michal Charemza
  • 25,940
  • 14
  • 98
  • 165
  • Thank you. I thought it was the same as angular.module(), so I was looking at that API, not ngMock. Not evident at first sight. – Laszlo B May 28 '15 at 06:33