Lesson Weekend

In this lesson, we'll connect our test project to the test database and update our teardown method to empty it.

Using the Test Database


First, let's run dotnet restore in our ToDoList.Tests directory to update the Tests project with the MySqlConnector package that we added to the production project.

Next, let's refactor our existing ItemTests.cs file.

ToDoList.Tests/ModelTests/ItemTests.cs
using MySql.Data.MySqlClient;

...

namespace ToDoList.Tests
{

  [TestClass]
  public class ItemTests : IDisposable
  {

    public void Dispose()
    {
      Item.ClearAll();
    }

    // The method below is new code.
    public ItemTests()
    {
      DBConfiguration.ConnectionString = "server=localhost;user id=root;password=epicodus;port=3306;database=to_do_list_test;";
    }

    // existing tests here
    ...

  }
}
  • First, we need to make sure we are using MySql.Data.MySqlClient;.

  • Next, we set DBConfiguration.ConnectionString to our to_do_list_test database in an ItemTests constructor. This overrides the DBConfiguration.ConnectionString we set in Startup.cs. This ensures our tests are connected to our test database, not our development database. Our ClearAll() method isn't programmed to interact with the database yet, but we'll tackle that in an upcoming lesson.

Repository Reference

Follow the link below to view how a sample version of the project should look at this point. Note that this is a link to a specific commit in the repository.

Example GitHub Repo for To Do List

Lesson 9 of 36
Last updated more than 3 months ago.