You can test an anonymous controller. Instead of passing the controller identifier to the $controller(...) function, you need to pass the controller constructor array.
To do this, you need to make the controller code available for your unit test code. This is often done using a modulation tool for JavaScript (e.g. Webpack). But this can be done in the settings of your testing and <script></script> . I recommend using the tool.
Despite this, you get three files:
// delete-item-controller.js DeleteItemController = ['$scope', '$modalInstance', 'someService', function($scope, $modalInstance, someService) { .... } ];
// state-controller.js $scope.deleteSomething = function() { $modal.open( templateUrl: '/delete-item-modal.html', controller: DeleteItemController, resolve: { ... } }); };
// delete-item-controller.spec.js beforeEach(inject(function($controller) { $controller(DeleteItemController, {/*dependancies to be injected*/}); }));
Alternative example using a module loader
Using Webpack would look a little different ... but it eliminates the fuzzy, implied global object that holds onto DeleteItemController .
// delete-item-controller.js module.exports = ['$scope', '$modalInstance', 'someService', function($scope, $modalInstance, someService) { .... } ];
// state-controller.js var DeleteItemController = require("./delete-item-controller"); $scope.deleteSomething = function() { $modal.open( templateUrl: '/delete-item-modal.html', controller: DeleteItemController, resolve: { ... } }); };
// delete-item-controller.spec.js var DeleteItemController = require("./delete-item-controller"); beforeEach(inject(function($controller) { $controller(DeleteItemController, {/*dependancies to be injected*/}); }));