[英]Unit testing with jasmine
I'm new to jasmine. 我是茉莉的新手。 Here is my directive 这是我的指令
(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();
}
});
};
}
}
})(); })();
Here I've tested my controller and the function in it to be defined. 在这里,我已经测试了控制器及其要定义的功能。 In the 3rd It block, I want to test if the emailApi.sendForgotPasswordEmail function is invoked with (response.data.EmailAddress, response.data.FirstName, response.data.LastName, response.data.QueryString) arguments 在第三个It块中,我想测试是否使用(response.data.EmailAddress,response.data.FirstName,response.data.LastName,response.data.QueryString)参数调用emailApi.sendForgotPasswordEmail函数。
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 () {
});
}); });
Thanks for help guys, I really cant figure out 谢谢你们的帮助,我真的不知道
Account.customerForgotPassword(vm.email)
seems to be returning a promise so you can do something like this Account.customerForgotPassword(vm.email)
似乎正在返回承诺,因此您可以执行以下操作
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.