question

Aaronsoggi-6095 avatar image
0 Votes"
Aaronsoggi-6095 asked karenpayneoregon answered

Unit testing in visual studio

Hi, for my project i have to do unit testing in visual studio. I'm using the baked in SQL server. How would i do this?

I've tried creating a unitTest project and then create an instance of of my SQLDataFunction class which includes all of the references to my SQL operations but it just takes forever when loading. I'm stuck, and don't know what to do.


How would i test something like this:

 string query = " SELECT cs.coachScheduleId, cs.stationDeparture, cs.stationArrival, cs.timeOfDeparture, cs.timeOfArrival, cs.dateOfDeparture, c.numberOfSeats FROM coachSchedule cs JOIN Coach c ON cs.coachId = c.CoachId WHERE cs.dateOfDeparture BETWEEN @dateFrom AND @dateTo AND cs.stationDeparture= @travelFrom AND cs.stationArrival= @travelTo";
                                                   
                     cmd = new SqlCommand(query, _IsqlDataFunctions.GetConnection());
                     cmd.Parameters.AddWithValue("@dateFrom", dateFromPicker.Value);
                     cmd.Parameters.AddWithValue("@dateTo", dateTooPicker.Value);
                     cmd.Parameters.AddWithValue("@travelFrom", comboBoxTravelFrom.SelectedValue);
                     cmd.Parameters.AddWithValue("@travelTo", comboBoxTravelTo.SelectedValue);
        
                     _IsqlDataFunctions.displayDataInGrid(cmd, availableBookings);
        
                     if (availableBookings.Rows.Count == 0) 
                     {
                         MessageBox.Show("Unfortunately no bookings can be found with the information you have entered. Please try again.");
                     }


sql-server-generaldotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

@Aaronsoggi-6095

You should understand when a test is not a unit test, becuase accessing of a database is not a unit test, and you should not be trying to unit test code that is doing it.

https://www.artima.com/weblogs/viewpost.jsp?thread=126923

If that code is executed that you have it is considered integration testing, which means you're executing the code in a test that is doing something for real.

If this is being done for a company you work for, then the company should provide some unit testing classes for a basic understanding by the developer trying to do unit testing, IMHO.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

A unit test is done in a unit test project and one does not use things like MessageBox, instead you assert what is expected to what is returned.

Here is super simple example for some unit test that is in this case reference a class project with data operations which reads from a live development environment. Alternate is to mock-up data but that is not truly testing the database such as indexes and permissions for instance.

Now if things are running slow in unit test such as these examples, open SQL-Profiler to assist in learning why there is a slow response or in SSMS and look at the query plan to see if indexes are needed etc..

Random discussion on mocking

81988-f1.png

 namespace DataUnitTestProject
 {
     [TestClass]
     public class UnitTest1
     {
            
         [TestMethod]
         public void ReadAllRecordsFromMessagesTest() 
         {
             var operations = new ClaimMessagesOperations();
                
             Assert.IsNotNull(operations.BasicReadAllMessages(),
                 "Expected to read OCS_MESSAGE data");
         }
    
    
         [TestMethod]
         public void GetRecordByFieldNameAndLanguageTest()
         {
             var fieldValue = "PIN4";
             var languageCode = "EN";
    
             var operations = new ClaimMessagesOperations();
             var messageResult = operations.GetRecordByColumnNameAndLanguage(fieldValue, languageCode);
    
             Assert.AreEqual(messageResult.MessageTxt, "PIN needs to have 4 digits.");
         }

         [TestMethod]
         public void GetAllRecordByFieldNameAndLanguageTest() 
         {
             var operations = new ClaimMessagesOperations();
             var fieldValue = "PIN4";
             var test = operations.GetRecordsByColumnName(fieldValue);
    
             Assert.IsTrue(test.Count == 4);
         }
         [TestMethod]
         public void GetClaimByPrimaryKey()
         {

             decimal primaryKey = 1043;
    
             var operations = new ClaimsOperations();
             var claimRecord = operations.GetById(primaryKey);
    
             Assert.IsTrue(claimRecord
                 .MessageTxt
                 .StartsWith("You are claiming this week before restarting your claim."),
                 $"Expected to find {primaryKey} for GetById on ClaimMessage");
         }
     }
 }



f1.png (57.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.