0
Good morning guys, I need a help to do the unit test of a controller with Jasmine, my problem is that I need to cover the methods of this controller and I am not able to do the right way to get through all lines of code. The controller is this below.
'use Strict'; angular.module('cadastarUsuario.module') .controller('user control', Function (messagesService, sharedService, register Uservice) { var vm = this; vm.cadastrarUsuario = _cadastrarUsuario; vm. vm.dataCadastro = { indicatorExistence: ' }; vm.data vm.TELAS = sharedService.TELAS; vm.tela = vm.TELAS.INICIO; vm.confirm = _confirm; vm.back = _back;
function _cadastrarUsuario() {
cadastarUsuarioService.consultarCPF(vm.dados)
.then(function (dados) {
if (dados.indicadorExistencia == 0) {
vm.dadosCadastro = dados.titulares[0];
}
vm.dadosCadastro.indicadorExistencia = dados.indicadorExistencia;
sucessoTransacao(vm.TELAS.CONFIRMACAO);
}, function (e) {
mensagemService.error(e.error);
});
}
function sucessoTransacao(tela) {
vm.tela = tela;
_subirRolagem();
}
function _subirRolagem() {
window.parent.scroll(0, 0);
}
function _confirmar() {
cadastarUsuarioService.cadastrarNovoUsuario(vm.dadosCadastro)
.then(function (dados) {
vm.dadosResposta = dados;
sucessoTransacao(vm.TELAS.EFETIVACAO);
}, function (e) {
mensagemService.error(e.error);
});
}
function _voltar() {
vm.tela = vm.TELAS.INICIO;
}
});
At the exit of the corevage is getting like this:
I don’t know how to make the code to go through as many lines as possible in this code, could someone please put an example of how to write this test? I would be very grateful, remembering that I am beginner with tests with Jasmine and still do not have all the necessary knowledge to do this test.
The test I wrote was like this, this was as far as I could get, which was to get to the . then of _cadastrarUsuario.
describe('', Function () {
beforeEach(module('app.core'));
beforeEach(module('sharedService.mock'));
beforeEach(module('mensagemService.mock'));
beforeEach(module('cadastarUsuario.module'));
var usuarioController;
beforeEach(() => {
module(function($provide) {
$provide.service('serviceCfeHTTP', function() {
this.post = function(url) {
return 80;
}
});
});
//Inject da controller e das services mock utilizadas por ela.
inject(function ($q, _$controller_, _mensagemServiceMock_, _sharedServiceMock_, _cadastarUsuarioService_) {
q = $q.defer();
$controller = _$controller_;
mensagemServiceMock = _mensagemServiceMock_;
sharedServiceMock = _sharedServiceMock_;
cadastarUsuarioService = _cadastarUsuarioService_;
});
//Referencia das services mock usadas pela controller
usuarioController = $controller('cadastarUsuarioController', {
mensagemService: mensagemServiceMock,
sharedService: sharedServiceMock,
cadastarUsuarioService: cadastarUsuarioService,
});
});
describe('Teste da Usuario Controller', () => {
it('cadastarUsuarioController está definida?', () => {
expect(usuarioController).toBeDefined();
});
});
describe('Teste do cadastarUsuarioService', () => {
it('Se existe cadastrarUsuarioService?', () => {
expect(cadastarUsuarioService).toBeDefined();
});
it('Verifica ase a função consultaCPF existe?', () => {
spyOn(cadastarUsuarioService, 'consultarCPF').and.returnValue(q.promise);
usuarioController.cadastrarUsuario();
expect(cadastarUsuarioService.consultarCPF).toHaveBeenCalled();
});
});
describe('Teste da service cadastarUsuarioService', () => {
it('Testar Function consultarCPF', function (){
var numero = cadastarUsuarioService.consultarCPF();
expect(numero).toEqual(80);
});
it('Testar Function cadastrarNovoUsuario', function (){
var numero = cadastarUsuarioService.cadastrarNovoUsuario();
expect(numero).toEqual(80);
});
});
describe('Teste da mensagemService.', () => {
it('Se existe mensagemService?', () => {
expect(mensagemService).toBeDefined();
});
});
});//end describe