Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Right, following jl's suggestion, I end up with this code, which works very well. Clearly my javascript knowledge was a flaw, I hadn't really considered the fact this all lives in a global address space.</p> <p>First, I create a file "base_controller.spec.js". In this file I declare a global object:</p> <pre><code>var BaseController = { baseBeforeEach: function() { // mock Application to allow us to inject our own dependencies beforeEach(angular.mock.module('bProject')); // create the custom mocks on the root scope beforeEach(angular.mock.inject(function($rootScope, _$httpBackend_, SidebarService, $state, BreadcrumbService ){ //create an empty scope scope = $rootScope.$new(); scope.$rootScope = $rootScope; scope.$state = $state; scope.breadcrumb = BreadcrumbService; // we're just declaring the httpBackend here, we're not setting up expectations or when's - they change on each test scope.httpBackend = _$httpBackend_; // setup the project id to a known value scope.setupSidebar = SidebarService.sidebar; scope.setupSidebar.projectId = 2; })); }, baseAfterEach: function() { afterEach(function() { scope.$digest(); scope.httpBackend.verifyNoOutstandingExpectation(); scope.httpBackend.verifyNoOutstandingRequest(); }); } }; </code></pre> <p>Then, I just use BaseController in my tests:</p> <pre><code>describe( 'Risk functionality.', function() { BaseController.baseBeforeEach(); BaseController.baseAfterEach(); describe( 'Risks list controller.', function() { </code></pre> <p>This example is reasonably trivial, although having the mocking standard on each controller is nice. But I have a set of standard tests I do on every controller, and this method will let me do it.</p> <p>Something to watch out for is the order of the includes in your karma configuration - you need to include this file early or else some modules cannot see it.</p> <p>I'd give jl an upvote, but it was a comment, so I can't!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload