简体   繁体   中英

Unit Test for CreateDatabaseConnection method

Using: Microsoft Unit Testing Framework integrated with VS2010

I have a class that implements this interface.

public interface IConnectionManager
{
    IDbConnection OpenDatabase(string path);
    void CloseDatabase();
}

I would like to create a set of Tests for these methods but not quite sure how to proceed.

What is the best way to test this?

Thanks.

EDIT:

My OpenDatabase implementation looks something like this:

    public OleDbConnection OpenDatabase (string p_path)
    {
        if (Library.StringOperations.IsNullOrEmpty (p_path))
            return null;

        bool error = false;
        string  connectionString= @"CONNECTION STRING HERE";
        try
        {
            OleDbConnection con= new OleDbConnection (connectionString);
            con.Open ();
        }
        catch (Exception)
        {
            error = true;
        }

        if (!error)
            return con;

        return null;
    }

As suggested by @rdkleine I want to test the returned connection.

A few questions:

  1. Do I need to create a new OleDbConnection object to compare with my returned object or should I check my returned connection object proprieties?

  2. Can Mock Objects be used in this case?

  3. Having in mind that I am testing a connection to a DB is this still Unit Testing or Integration Testing ?

Thanks again.

Start simple.

  • Test returns connection
  • Test path exists
  • Test path doesn't exist
  • Test can't connect
  • Test can connect

etc

Read this book about TDD :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM