Skip to content

"dumb" spec inject function rule #213

@remcohaszing

Description

@remcohaszing

Typically an AngularJS test file looks similar to this.

describe('kittenService', function() {
  var $httpBackend;
  var kittenService;

  beforeEach(module('myApp'));

  beforeEach(inject(function(_$httpBackend_, kittenService) {
    $httpBackend = _$httpBackend_;
    $httpBackend.whenGET(/kittens/).respond([]);
    kittenService = kittenService;
    kittenService.initialize();
  }));

  it(...);
});

I think it is more readable to keep the dependency injection separated from the other setup logic.

describe('kittenService', function() {
  var $httpBackend;
  var kittenService;

  beforeEach(module('myApp'));

  // Inject dependencies
  beforeEach(inject(function(_$httpBackend_, _kittenService_) {
    $httpBackend = _$httpBackend_;
    kittenService = _kittenService_;
  }));

  // Do initial setup which might be messy in some cases.
  beforeEach(function() {
    $httpBackend.whenGET(/kittens/).respond([]);
    kittenService.initialize();
  });

  it(...);
});

This rule comes down to: If inject is called, the passed function may only contain (sorted) statements of the form arg = _arg_.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions