angularjs - Writing units tests for a Angular Controller -
my controller looks
angular.module('demo').controller('democtrl',['$scope','$modal', function($scope, $modal){ $scope.askforinput = function(action){ var modalinstance = $modal.open({ /* here open modal , display form input */ }); modalinstance.result.then(function(input){ /* process input here */ }); }; }
the part confused how test code i.e test modal opened, form filled , data received in controller ?
i using jasmine , stuck on first line itself.
i wouldn't recommend testing $modal
, instead democtrl
, askforinput
should do. when you're creating controller in test, can inject mock values $scope
, $modal
. because you're injecting mocks, have complete control on how behave.
for instance, follows:
// dependencies var $scope, $modal, controller; // test values var modalopeninput, modalinstance; beforeeach(module("demo")); beforeeach(inject(function($rootscope, $controller) { $scope = $rootscope.$new(); modalinstance = { result: { then: function(callback) { // define here... } } }; $modal = { open: function(input) { // storing you're able later // confirm it's value set modalopeninput = input; return modalinstance; } }; controller = $controller("democtrl", { $scope: $scope, $modal: $modal }); })); it("should it's supposed do...", function() { $scope.askforinput(); // test function works defined... expect(modalopeninput).tobedefined(); // etc... });
hopefully gives idea on how move forward. can define multiple paths based on input, , verify each path works defined. since have access mocks, you're able define how $modal
, it's functions behave.
Comments
Post a Comment