NullReferenceException when calling function

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 from   Test: DataAccessLayerTest.PostgreSqlDataAccessTest.TestCallFunction   Test Source: C: \ Users \ Matheus   Hail \ OneDrive \ Development \ Systems \ DataAccessLayer \ DataAccessLayer \ DataAccessLayerTest \ PostgreSqlDataAccessTest.cs:   line 39 Test result: Failed Duration of test   Test: 0: 00: 00,1025821

     

Result StackTrace: in   DataAccessLayerTest.PostgreSqlDataAccessTest.TestCallFunction () on   C: \ Users \ Matheus   Hail \ OneDrive \ Development \ Systems \ DataAccessLayer \ DataAccessLayer \ DataAccessLayerTest \ PostgreSqlDataAccessTest.cs: line   45 Result message: Test method   DataAccessLayerTest.PostgreSqlDataAccessTest.TestCallFunction generated   Exception: System.NullReferenceException: Object Reference Does Not   defined for an instance of an object.

I can not identify the error.

    
asked by anonymous 04.06.2016 / 20:01

1 answer

4

If this is a "formal" test, you can not access what you understand. Actually a test should do the bare minimum. Often it's just the line of Assert itself.

This test does not seem to make sense. Starting with the name. It does not tell you what you are testing.

If it's not a test, which it does not look like, the problem is elsewhere in the class that was not posted.

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

Any of the main method's code is very complicated. It would be something more like this (I do not know if this is well, I did not test and I do not know the context well):

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();
            }
        }
    }
}

Do not catch an exception to do nothing useful. Prefer to use using to allocate resources. I used it to make it easier to read.

Some questions to help you understand the subject:

04.06.2016 / 20:37