I'm in the process of trying to teach myself some C# (used to code in C++ in the late 90's if that tells you anything). So anyway, I keep running into a test failure in Visual Studio 2017 when attempting to test one of my classes.
The setup is as follows:
And within my ErrorLog.cs file:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Configuration;
namespace ManagementLibrary {
public class ErrorLog {
private int log_id { get; set; }
private string logException {get; set;}
private string error { get; set; }
private string date_time { get; set; }
private string logObject { get; set; }
public ErrorLog(string logException, string error, string date_time, string logObject) {
this.logException = logException;
this.error = error;
this.date_time = date_time;
this.logObject = logObject;
}
public int LogError() {
using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("DataConnection"))) {
SqlCommand command = new SqlCommand("InsertErrorLog", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter();
p1.ParameterName = "@Exception";
p1.SqlDbType = SqlDbType.VarChar;
p1.Direction = ParameterDirection.Input;
p1.Value = this.logException;
SqlParameter p2 = new SqlParameter();
p2.ParameterName = "@Error";
p2.SqlDbType = SqlDbType.VarChar;
p2.Direction = ParameterDirection.Input;
p2.Value = this.error;
SqlParameter p3 = new SqlParameter();
p3.ParameterName = "@DateTime";
p3.SqlDbType = SqlDbType.VarChar;
p3.Direction = ParameterDirection.Input;
p3.Value = this.date_time;
SqlParameter p4 = new SqlParameter();
p4.ParameterName = "@logObject";
p4.SqlDbType = SqlDbType.VarChar;
p4.Direction = ParameterDirection.Input;
p4.Value = this.logObject;
command.Parameters.Add(p1);
command.Parameters.Add(p2);
command.Parameters.Add(p3);
command.Parameters.Add(p4);
connection.Open();
var identity = command.ExecuteNonQuery();
return identity;
}
}
}
}
Essentially I'm trying to create an audit trail for when an error is encountered it simply adds a record to the database. So not only am I attempting to teach myself some C#, but I'm trying to learn how to do unit testing within Visual Studio. I've followed some of the tutorials on setting up and doing unit tests, but when I do I keep getting the following failure:
Message: Test method ManagementLibrary.Tests.ErrorLogTests.LogErrorTest threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
And when I "debug" the failure it "goes to" my Helper.cs file. Now this class simply reads the App.config file and returns the connection string.
namespace ManagementLibrary {
public static class Helper {
public static string CnnVal(string name) {
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}
}
}
And shows the error: 'Object reference not set to an instance of an object'.
I don't even know what this means.
Thoughts?

app.configfile to the test project and copy over the connection string section from the main project'sapp.config. The error is because the test runs in its own application domain context and is unable to find the requested connection string.