我是编写单元测试案例的新手。我正试图在整个RefferenceError中为这个模块页面编写单元测试。(这里我放置了module.spec.js和module.js文件)
forgot-password.module.spec.js
describe('Forgot Password Routing', function(){
var controller;
var view = 'app/main/auth/forgot-password/forgot-password.html';
beforeEach(function(){
module('app.auth.forgot-password', bard.fakeToastr);
bard.inject(this, '$location', '$route', '$rootScope', '$templateCache');
$templateCache.put(view, '');
});
it('should map / route to dashboard View template', function () {
expect($route.routes['/'].templateUrl).
to.equal('app/main/auth/forgot-password/forgot-password.html');
});
it('should route / to the dashboard View', function () {
$location.path('/');
$rootScope.$apply();
expect($route.current.templateUrl).to.equal(view);
});
it('should route /invalid to the otherwise (dashboard) route', function () {
$location.path('/invalid');
$rootScope.$apply();
expect($route.current.templateUrl).to.equal(view);
});
})
忘记-password.module.js:
(function ()
{
'use strict';
angular
.module('app.auth.forgot-password', [])
.config(config);
/** @ngInject */
function config($stateProvider, $translatePartialLoaderProvider, msNavigationServiceProvider, msApiProvider)
{
// State
$stateProvider.state('app.auth_forgot-password', {
url : '/auth/forgot-password',
views : {
'main@' : {
templateUrl: 'app/core/layouts/content-only.html',
controller : 'MainController as vm'
},
'content@app.auth_forgot-password': {
templateUrl: 'app/main/auth/forgot-password/forgot-password.html',
controller : 'ForgotPasswordController as vm'
}
},
bodyClass: 'forgot-password'
});
// Translation
$translatePartialLoaderProvider.addPart('app/main/auth/forgot-password');
}
})();
忘记-password.controller.js:
(function ()
{
'use strict';
angular
.module('app.auth.forgot-password')
.controller('ForgotPasswordController', ForgotPasswordController);
/** @ngInject */
function ForgotPasswordController(BRANDING, forgotPasswordService, msUtils)
{
var vm = this;
vm.branding = BRANDING;
vm.form = {};
vm.resetPasswordSuccess = false;
vm.resetPasswordSuccessMsg = '';
vm.resetPasswordError = false;
vm.resetPasswordErrorMsg = '';
// Data
//vm.passwordToken = PasswordToken.data;
vm.forgotPassword = forgotPassword;
vm.resend = resend;
// Methods
function forgotPassword() {
var _email = vm.form.email;
vm.resetPasswordError = false;
// vm.resetPasswordSuccess = false;
forgotPasswordService.initiateForgotPassword(_email,
function (response) {
vm.resetPasswordMsg = response.message;
vm.resetPasswordSuccess = true;
msUtils.setStore('users', JSON.stringify(response.data));
},
function (response) {
vm.resetPasswordError = true;
vm.resetPasswordErrorMsg = response.data.message;
}
);
}
function resend() {
vm.resetPasswordSuccess = true;
forgotPassword();
}
//////////
}
})();
忘记-password.service.js:
(function() {
'use strict';
angular
.module('app.core')
.service('forgotPasswordService', forgotPasswordService);
/** @ngInject */
function forgotPasswordService($q, msApi, API_BASE, HTTP_HOST, RESET_PASSWORD_ENDPOINT, PRODUCT_NAME)
{
var service = {
initiateForgotPassword: initiateForgotPassword,
changePassword: changePassword
};
return service;
//////////
/**
* Reset password and send the reset link to the email
*
* @returns {*}
*/
function initiateForgotPassword(username, success, error)
{
// Emulate the api call and load new timeline items in
var deferred = $q.defer();
var parameters = {};
parameters.username = username;
parameters.product_name = PRODUCT_NAME;
parameters.password_recovery_link = HTTP_HOST + RESET_PASSWORD_ENDPOINT;
if ( !username )
{
// Reject the promise
deferred.reject('No more pages');
} else {
msApi.setBaseUrl(API_BASE);
msApi.register('auth.forgot.password', ['api/admins/{admin_id}/password']);
msApi.requestApi('auth.forgot.password@post', parameters,
// Success
function (response)
{
// Resolve the promise
deferred.resolve(response);
// Call the success function if there is one
if ( angular.isDefined(success) && angular.isFunction(success) )
{
success(response);
}
},
// ERROR
function (response)
{
// Reject the promise
deferred.reject(response);
// Call the error function if there is one
if ( angular.isDefined(error) && angular.isFunction(error) )
{
error(response);
}
}
);
}
return deferred.promise;
}
/**
* Get registered palettes
*
* @returns {*}
*/
function changePassword(parameters, success, error)
{
// Emulate the api call and load new timeline items in
var deferred = $q.defer();
if ( !parameters )
{
deferred.reject('No more pages');
} else {
msApi.setBaseUrl(API_BASE);
msApi.register('auth.reset.password', ['api/admins/{admin_id}/password/{token}']);
msApi.requestApi('auth.reset.password@put', parameters,
// Success
function (response)
{
// Resolve the promise
deferred.resolve(response);
// Call the success function if there is one
if ( angular.isDefined(success) && angular.isFunction(success) )
{
success(response);
}
},
// ERROR
function (response)
{
// Reject the promise
deferred.reject(response);
// Call the error function if there is one
if ( angular.isDefined(error) && angular.isFunction(error) )
{
error(response);
}
}
);
}
return deferred.promise;
}
}
})();