简体   繁体   中英

How do I refactor this C# SQL query to do a unit test?

I have this code that queries a database. I want to put the actual database code into a separate class so I can reuse it in other places. This will leave just the actual read of the PassResult value so I can make a Unit Test of the code without having the SQL code running. I am having trouble finding references on how to make this kind of code Unit Testable. Could someone help out?

            using System;
            using System.Data;
            using System.Data.SqlClient;

            namespace CS_UI_Final_Inspection
            {
                public class CalibrationTestCheck
                {
                    // declare the variables
                    private bool _calibrationTestPass = false;
                    private string _connectionString = string.Empty;

                    public bool CheckCalibrationTestResults(string serialNumber, IDeviceInfo deviceInfo, string mapID)
                    {
                        // get database location
                        DhrLocationPull dhrLocation = new DhrLocationPull();
                        _connectionString = dhrLocation.PullDhrLocation();

                        // build the query
                        SqlConnection calibrationCheckConnection = new SqlConnection(_connectionString);
                        SqlCommand calibrationCheckCommand = new SqlCommand("[MfgFloor].[GetLatestTestResultsForDeviceByTestType]",
                            calibrationCheckConnection);

                        // build the stored proc
                        calibrationCheckCommand.CommandType = CommandType.StoredProcedure;
                        calibrationCheckCommand.Parameters.Add(new SqlParameter("@SerialNumber", serialNumber));
                        calibrationCheckCommand.Parameters.Add(new SqlParameter("@DeviceTypeID", mapID));
                        calibrationCheckCommand.Parameters.Add(new SqlParameter("@TestDataMapTypeID", "C"));
                        calibrationCheckCommand.Connection.Open();
                        SqlDataReader calibrationCheckReader = calibrationCheckCommand.ExecuteReader();

                        // is there data?
                        if (calibrationCheckReader.HasRows)
                        {
                            // read the data
                            calibrationCheckReader.Read();
                            try
                            {
                                _calibrationTestPass = (bool) calibrationCheckReader["PassResult"];
                            }
                            catch (InvalidOperationException)
                            {
                                // means last element was not filled in
                            }
                            finally
                            {
                                // close refs
                                calibrationCheckReader.Close();
                                calibrationCheckCommand.Connection.Close();
                                calibrationCheckConnection.Close();
                                calibrationCheckReader.Dispose();
                                calibrationCheckCommand.Dispose();
                                calibrationCheckConnection.Dispose();
                            }
                        }
                        return _calibrationTestPass;
                    }
                }
            }
  1. create an interface and implement it.
  2. move all references to be tested to use the interface (exposing any methods/properties required through the interface)
  3. have the constructor or method being tested take the interface as a parameter.

Roy Oscherov is a good resource on this. Roy Oscherov wrote a great book called "The art of unit testing". Roy's website can be found here: http://osherove.com/

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