2017-05-16 10 views
0

Ich bin neu in Jasmin. Hier ist meine DirektiveUnit Testing mit Jasmin

(function() { 
'use strict'; 
angular.module('AccountPortal.components').directive('forgotPasswordForm', forgotPasswordForm); 
forgotPasswordForm.$inject = ['Account', 'accountApi', 'emailApi', 'utils', 'Exception']; 
function forgotPasswordForm(Account, accountApi, emailApi, utils, Exception) { 

    var directive = {}; 
    directive.restrict = 'E'; 
    directive.templateUrl = 'app/components/_utilities/forgotPasswordForm/forgotPasswordForm.html'; 
    directive.controller = ['$scope', ctrl]; 
    directive.controllerAs = 'vm'; 
    directive.scope = {}; 

    return directive; 

    function link(scope, elem, attrs, vm) { 

    } 

    function ctrl($scope) { 
     var vm = this; 
     vm.formSubmitted = false; 
     vm.email = ''; 
     vm.forgotPasswordSubmit = function() { 
      if (!vm.forgotPasswordForm.$valid) { 
       return; 
      } 
      delete vm.forgotPasswordForm.email.$error.accountNotFound; 
      Account.customerForgotPassword(vm.email) 
       .then(function (response) { 
        emailApi.sendForgotPasswordEmail(response.data.EmailAddress, response.data.FirstName, response.data.LastName, response.data.QueryString); 
        vm.formSubmitted = true; 
       }) 
       .catch(function (response) { 
        if (response.status === 400) { 
         $scope.forgotPasswordForm.email.$error.accountNotFound = true; 
        } 
        else { 
         throw new Exception(); 
        } 
       }); 
     }; 
    } 
} 

})();

Hier habe ich meinen Controller getestet und die Funktion darin definiert. Im 3. Es Block, möchte ich, wenn die emailApi.sendForgotPasswordEmail Funktion mit (response.data.EmailAddress, response.data.FirstName, response.data.LastName, response.data.QueryString) Argumente

describe('forgotPasswordForm directive', function() { 
var ctrl,emailApi, $componentController; 
beforeEach(module('AccountPortal')); 
beforeEach(inject(function (_$componentController_, _emailApi_) { 
    $componentController = _$componentController_; 
    ctrl = $componentController('forgotPasswordForm'); 
    emailApi=_emailApi_; 
    ctrl.forgotPasswordForm = { 
     $valid: true, 
     email: { 
      $error:{} 
     } 
    }; 
})); 

beforeEach(inject(function(_Account_) { 
    Account = _Account_; 
})); 


it('forgotPasswordForm controller should be defined', function() { 
    expect(ctrl).toBeDefined(); 
}); 

it('forgotPasswordSubmit function should be defined', function() { 
    expect(ctrl.forgotPasswordSubmit).toBeDefined() 
}); 

it('new password should be sent if email exists', function() { 


}); 
aufgerufen wird, testen

});

Dank für die Hilfe Jungs, ich kann wirklich nicht figure out

Antwort

0

Account.customerForgotPassword(vm.email) scheint ein Versprechen der Rückkehr zu werden, so dass Sie so etwas wie dieses

spyOn(Account,'customerForgotPassword').and.callFake(function(){ 
      return new Promise(function(resolve,reject){ 
       resolve({ 
         data:{'firstname':'xx', 
           'emailAddress':'yy', 
           'LastName':'zz', 
           'queryString':'aa' 
           } 
       }) 
      }) 
     }) //This will return any data you want to the then function 
    spyOn(emailApi,'sendForgotPasswordEmail'); 
    //call the ctrl function 
    expect(emailApi.sendForgotPasswordEmail).toHaveBeenCalledWith('yy','xx','zz','aa') 
+0

tun kann Das ist nicht für mich arbeiten. Uncaught Expected Spion sendForgotPasswordEmail wurde mit ['yy', 'xx', 'zz', 'aa'] aufgerufen, wurde aber nie aufgerufen. Danke trotzdem. –