angularjs - Mocking service for Jasmine testing? -


i new jasmine testing , not solid picture how unittest controller. controller assignments , want test following:

  1. mock service calls.
  2. mock data in scope.
  3. test behaviour of particular method in controller.
  4. verify that: service has been invoked once.
  5. verify that: service passed correct data.

i need have complete possible example how write test, , declarations use mock methods.

controller method in question looks this:

.controller('projectconfigurationctrl', function($scope, $routeparams, infraservice, projectconfigurationservice) {   // pre-init...    $scope.clickupdate = function() {      if ($scope.projectdata.id === 0) {        projectconfigurationservice.save(angular.tojson($scope.projectdata), function(project) {       $scope.projectdata = project;       $scope.newproject = false;       $scope.info = "project " + project.projectname + " created successfully.";         }, function() {       $scope.info = "project " + $scope.projectdata.projectname + " can not created: server error.";       });       } else {         projectconfigurationservice.update(angular.tojson($scope.projectdata), function() {         $scope.info = "project " + $scope.projectdata.projectname + " updated successfully.";         }, function() {         $scope.info = "project " + $scope.projectdata.projectname + " can not updated: server error.";   }); } 

i want see if projectconfigurationservice.save/update called once, , check arrived these calls parameters. want mocked service return information. after call, want examine $scope.newproject, $scope.info etc. see inside there.

see answer in angular unit testing on controller using service - breaking on inject reference jasmine test. can search around web more examples if needed.

i pleased see idea in "unit-test" correct keep going. key thing is, have inject $controller controller constructor service, allowing pass mocked dependencies. in case, in "it" function should have like:

var projectconfigctrl = $controller('projectconfigurationctrl', // name of controller      /* mocked dependencies */ {$scope: <yourmockedscope, $rootscope.$new>,                                 $routeparams: <your mock>,                                 infraservice: <your mock service>,                                 projectconfigurationservice: <your mock service>}); 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -