Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  27] [ 2]  / answers: 1 / hits: 16288  / 10 Years ago, mon, june 23, 2014, 12:00:00

This is a somewhat of a follow-on question to this one: Mocking $modal in AngularJS unit tests



The referenced SO is an excellent question with very useful answer. The question I am left with after this however is this: how do I unit test the modal instance controller? In the referenced SO, the invoking controller is tested, but the modal instance controller is mocked. Arguably the latter should also be tested, but this has proven to be very tricky. Here's why:



I'll copy the same example from the referenced SO here:



.controller('ModalInstanceCtrl', function($scope, $modalInstance, items){
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};

$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};

$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});


So my first thought was that I would just instantiate the controller directly in a test, just like any other controller under test:



beforeEach(inject(function($rootScope) {
scope = $rootScope.$new();
ctrl = $controller('ModalInstanceCtrl', {$scope: scope});
});


This does not work because in this context, angular does not have a provider to inject $modalInstance, since that is supplied by the UI modal.



Next, I turn to plan B: use $modal.open to instantiate the controller. This will run as expected:



beforeEach(inject(function($rootScope, $modal) {
scope = $rootScope.$new();
modalInstance = $modal.open({
template: '<html></html>',
controller: 'ModalInstanceCtrl',
scope: scope
});
});


(Note, template can't be an empty string or it will fail cryptically.)



The problem now is that I have no visibility into the scope, which is the customary way to unit test resource gathering, etc. In my real code, the controller calls a resource service to populate a list of choices; my attempt to test this sets an expectGet to satisfy the service my controller is using, and I want to validate that the controller is putting the result in its scope. But the modal is creating a new scope for the modal instance controller (using the scope I pass in as a prototype), and I can't figure out how I can get a hole of that scope. The modalInstance object does not have a window into the controller.



Any suggestions on the right way to test this?



(N.B.: the behavior of creating a derivative scope for the modal instance controller is not unexpected – it is documented behavior. My question of how to test it is still valid regardless.)


More From » angularjs

 Answers
3

I test the controllers used in modal dialogs by instantiating the controller directly (the same way you initially thought to do it above).



Since there there's no mocked version of $modalInstance, I simply create a mock object and pass that into the controller.



var modalInstance = { close: function() {}, dismiss: function() {} };
var items = []; // whatever...

beforeEach(inject(function($rootScope) {
scope = $rootScope.$new();
ctrl = $controller('ModalInstanceCtrl', {
$scope: scope,
$modalInstance: modalInstance,
items: items
});
}));


Now the dependencies for the controller are satisfied and you can test this controller like any other controller.



For example, I can do spyOn(modalInstance, 'close') and then assert that my controller is closing the dialog at the appropriate time.


[#70465] Friday, June 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
breap questions
Thu, Jun 24, 21, 00:00, 3 Years ago
Wed, Mar 18, 20, 00:00, 4 Years ago
Mon, Oct 7, 19, 00:00, 5 Years ago
;