question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing Controllers with Injected Services

See original GitHub issue

hi.
In the spec example of “home” component, a controller is created with the new operator. what if, a controller is injected with services? how should that be addressed? i.e.:

// Given the controller is injected with these:
class HomeController {
  constructor(Users, $http, $stateParams) {
    this.name = 'home';
  }
}

export default HomeController;

Should I use the $controller in the injector?

beforeEach(inject((_$rootScope_, $controller) => {
    $rootScope = _$rootScope_;
    scope = $rootScope.$new();
    makeController = () => {
      return $controller('HomeController as vm', { $scope: scope });
    };
  }));

Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
aneurysmjscommented, Dec 18, 2015

@orizens , like @fesor said will be something like this:

beforeEach(inject((homeService, $state) => {
      makeController = () => {
         // you should instantiate your controller with 'new' operator and pass dependencies into constructor.
         return new HomeController(homeService, $state);
      };
}));

// you only use $controller when you register a controller with a module and you have to mock the depency:
// your service
function homeFactory() {
    var items = [
      {id: 1, label: 'Item 0'},
      {id: 2, label: 'Item 1'}
    ];
    return {
      list() {
        return items;
      }
    };
  }
export default homeFactory;

// your controller
class HomeController {
  constructor(homeFactory) {
    this.homeFactory = homeFactory;
    this.items = this.homeFactory.list();
  } 
}
export default HomeController;

// then the module
import angular from 'angular';
import HomeController from './home.controller'
import homeFactory from './home.factory'

angular.module('app', [])
  .factory('homeFactory', homeFactory)
  .controller('HomeController', HomeController);

// so the test will be
describe('HomeController testing', () => {
  beforeEach(module('app'));

  var ctrl, mockFactory;
  // mock out the service/dependency
  beforeEach(module(($provide) => { 
    mockFactory = {
      list: function() {
        return [{id: 1, label: 'Mock'}];
    }
  };
  $provide.value('homeFactory', mockFactory);
  }));

  beforeEach(inject(($controller) => {
    ctrl = $controller('HomeController');
  }));

  it('should load mocked out items', () => {
    expect(ctrl.items).toEqual([{id: 1, label: 'Mock'}]);
  });
});
0reactions
orizenscommented, Dec 20, 2015
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Testing With Dependency Injection On controller
I want to test my modification to AccountController (asp.net core 2 mvc) i use a repository to put logic this repo. The problem...
Read more >
Unit test controller logic in ASP.NET Core - Microsoft Learn
Set up unit tests of controller actions to focus on the controller's behavior. A controller unit test avoids scenarios such as filters, routing, ......
Read more >
Testing Controllers with Unit Tests and Moq in ASP.NET Core -
In this article, we are going to talk about unit testing controllers by using the Moq library, which enables us to isolate dependencies....
Read more >
Testing your controllers when you have a decoupled core
We need a way to inject this mock into the controller as a constructor argument. This should only happen when the controller is...
Read more >
How to test a controller with a service that has @Inject in its ...
I have a question regarding testing as described on https://docs.nestjs.com/advanced/unit-testing: beforeEach(async () => { const module ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found