我正在尝试使用Jasmin测试用AngularJS编写的小型控制器。
(function() {
'use strict';
angular
.module('bsp.account')
.controller('Account', Account);
/* @ngInject */
function Account(userService, accountService) {
var vm = this;
vm.title = 'Account';
vm.username = userService.getUsername();
vm.showPasswordModal = accountService.showPasswordModal;
vm.showLogoutModal = accountService.showLogoutModal;
}
})();
我想测试vm.username,vm.showPersonModal和vm.showLogoutModal.these是对控制器中注入的服务的所有引用。 我很新,慢慢地试图在测试中构建我的概念。 下面是现在运行的测试用例,
describe('Account', function() {
var scope, controller, userServiceMock,accountServiceMock;
beforeEach(module('bsp'));
beforeEach(function() {
userServiceMock = {
getUsername: function(){}
};
accountServiceMock = {
showPasswordModal :function(){}
};
});
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('Account', {
'userService': userServiceMock,
'accountService':accountServiceMock
});
}));
describe('testing Title',function(){
it('checkTitle', function(){
expect(controller.title).toEqual('Account');
});
});
});
感谢您的所有建议
答案 0 :(得分:0)
只有我能看到的问题是
'bsp.account'
而不是'bsp'
)您可以使用spies解决后者问题。例如
describe('Account', function() {
var controller, userServiceMock, accountServiceMock;
beforeEach(function() {
module('bsp.account');
userServiceMock = jasmine.createSpyObj('userService', ['getUsername']);
userServiceMock.getUsername.and.returnValue('testUser');
accountServiceMock = jasmine.createSpyObj('accountService', ['showPasswordModal', 'showLogoutModal']);
inject(function($controller) {
controller = $controller('Account', {
userService: userServiceMock,
accountService: accountServiceMock
});
});
});
it('assigns values from services', function() {
expect(userServiceMock.getUsername).toHaveBeenCalled();
expect(controller.username).toEqual('testUser');
expect(controller.showPasswordModal).toBe(accountServiceMock.showPasswordModal);
expect(controller.showLogoutModal).toBe(accountServiceMock.showLogoutModal);
});
});