2
I want to document a unit test but I’m getting error:
System.Exception: 'Error closing database connection: Undefined object reference for an instance of an object.'
My test case was so coded:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DAL.Model;
using DAL.Persistence;
namespace GErenciamentoTarefas_TesteUnitario
{
[TestClass]
public class UnitTest1
{
TarefaDAO td = new TarefaDAO();
int retorno1 = 0;
int retorno2 = 0;
int retorno3 = 0;
[TestMethod]
public void testeIncluirTarefa()
{
Tarefa tarefa1 = new Tarefa();
tarefa1.DataEntrega = "19/11/2018";
tarefa1.Nome = "Nome em Teste";
tarefa1.Responsavel = "Responsavel em Teste1";
tarefa1.Tipo = "1";
Tarefa tarefa2 = new Tarefa();
tarefa2.DataEntrega = "22/11/2018";
tarefa2.Nome = "Nome em Teste";
tarefa2.Responsavel = "Responsavel em Teste2";
tarefa2.Tipo = "2";
Tarefa tarefa3 = new Tarefa();
tarefa3.DataEntrega = "25/11/2018";
tarefa3.Nome = "Nome em Teste";
tarefa3.Responsavel = "Responsavel em Teste3";
tarefa3.Tipo = "3";
retorno1 = td.Gravar(tarefa1);
retorno2 = td.Gravar(tarefa2);
retorno3 = td.Gravar(tarefa3);
Assert.AreEqual(0, retorno1);
Assert.AreEqual(0, retorno2);
Assert.AreEqual(0, retorno3);
}
}
}
My connection class is so coded
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
namespace DAL.Persistence
{
public class ConexaoAccess
{
protected OleDbConnection Con;
protected OleDbCommand Cmd;
protected OleDbDataAdapter Da;
protected void AbrirConexao()
{
try
{
//Busca a string de conexão com o banco no arquivo Web.config
Con = new OleDbConnection(ConfigurationManager.ConnectionStrings["conexaoBanco"].ConnectionString);
Con.Open();
}
catch (Exception ex)
{
throw new Exception("Erro ao abrir conexão com banco de dados: " + ex.Message);
}
}
protected void FecharConexao()
{
try
{
Con = new OleDbConnection(ConfigurationManager.ConnectionStrings["conexaoBanco"].ConnectionString);
Con.Close();
}
catch (Exception ex)
{
throw new Exception("Erro ao fechar conexão com banco de dados: " + ex.Message);
}
}
}
}
I tried to correct by changing the position of the object OleDbConnection
:
public class ConexaoAccess
{
protected OleDbConnection Con = new OleDbConnection(ConfigurationManager.ConnectionStrings["conexaoBanco"].ConnectionString);
protected OleDbCommand Cmd;
protected OleDbDataAdapter Da;
but I was unsuccessful.
Debugger points out that the error is always when closing the connection:
What is the solution?
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site
– Maniero