Nullreferenceexception when calling function

Asked

Viewed 142 times

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.

1 answer

4


If this is a "formal" test you cannot access what you want. In fact in a test you should do the minimum necessary. Often it’s just the line of Assert even.

This test doesn’t seem to make sense. Starting with the name. It doesn’t say anything about what you’re testing.

If it’s not a test, which it doesn’t, the problem is elsewhere in the class that wasn’t posted.

Regardless of the problem the error occurs because the variable was not initialized, in case a pgDAL. The only difference is that in the test it does not have because it exists there, the line does not make sense, a test cannot depend on external things, and if it were a normal method it is only correct the error in the initialization of the variable. I can’t tell you the exact way without more context.

Of any the code of the main method is very complicated. It would be something more like this (I do not know if this is right, I have not tested and I do not know the context):

public object ExecuteNoQueryOperation(string spOrSqlInstructions, CommandType commandType = CommandType.StoredProcedure, Dictionary<string, object> parameters = null) {
    using var 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 { //eu deixei isso, mas não é uma boa ideia
        Connection.Open(); //precisa ter uma forma melhor de controlar abertura
        return npgsqlCommand.ExecuteScalar();
    } finally {
        if (Connection != null && Connection.State != ConnectionState.Closed) Connection.Close();
    }
}

I put in the Github for future reference.

Do not capture exception to do nothing useful. Prefer to use using to dispose resources. I took the opportunity to make it easier to read.

A few questions to help you understand the subject:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.