3
I’m creating some unit tests for a class library that does manipulations in a database.
The method being tested is this:
public object ExecuteNoQueryOperation ( string spOrSqlInstructions, CommandType commandType = CommandType.StoredProcedure, Dictionary<string, object> parameters = null )
{
NpgsqlCommand npgsqlCommand = new NpgsqlCommand(spOrSqlInstructions, Connection);
npgsqlCommand.CommandType = commandType;
if ( parameters != null )
foreach ( var item in parameters )
npgsqlCommand.Parameters.Add ( new NpgsqlParameter ( item.Key, item.Value ) );
try
{
Connection.Open ( );
return npgsqlCommand.ExecuteScalar ( );
}
catch ( NpgsqlException e )
{
throw new Exception ( "Error: " + e.ToString() );
}
finally
{
if ( Connection != null && Connection.State != ConnectionState.Closed )
Connection.Close ( );
npgsqlCommand.Dispose ( );
}
}
And here’s my test case:
[TestMethod]
public void TestCallFunction ( )
{
Dictionary<string, object> dic= new Dictionary<string, object> ();
dic.Add ( "f_name", "Joice Silva" );
dic.Add ( "f_age", 31 );
pgDal.ExecuteNoQueryOperation ( "insertPerson", parameters: dic);
Assert.AreEqual ( 31, ret);
}
While running this test I’m getting the error:
Test Name: Testcallfunction Fullname of Test: Dataaccesslayertest.PostgreSqlDataAccessTest.Testcallfunction Test Source: C: Users Matheus Saraiva Onedrive Development Systems Dataaccesslayer Dataaccesslayer Dataaccesslayertest Postgresqldataaccesstest.Cs: line 39 Test Result: with Failure Duration of Test: 0:00:00,1025821
Stacktrace of the Result: at Dataaccesslayertest.PostgreSqlDataAccessTest.Testcallfunction() na C: Users Matheus Saraiva Onedrive Development Systems Dataaccesslayer Dataaccesslayer Dataaccesslayertest Postgresqldataaccesstest.Cs:line 45 Result Message: Test method Dataaccesslayertest.PostgreSqlDataAccessTest.Testcallfunction generated exception: System.Nullreferenceexception: No object reference defined for an instance of an object.
I can’t identify the mistake.