1

I have a problem with running Unit tests in visual studio. I am doing a project that is a small system for VHS rental store. I was supposed to create a DB and create objects based on this data a insert them into the list where they will be processed.

enter image description here

Everything works correctly in "Data Layer" project. But when I try to create a second project in Visual Studio 2019 dedicated to Unit tests and i run the same part of code from "DataLayer/program.cs" in new project i get an error:

enter image description here

It looks as if Tests is unable to see where "Customers.db" is located and i do not know how to fix it. Here is my App.config from DataLayer project:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Default" connectionString="Data Source=.\Customers.db;" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Here is part of code that i want to test:

        SqliteDataAccess sqda = new SqliteDataAccess()
           var list = new System.Collections.Generic.List<Customer>(sqda.GetAllCustomersFromDb());
            var movies = new List<VhsMovie>(sqda.GetAllAvailableMoviesFromDb());
                            foreach (Customer cs in list)
            {

                Console.WriteLine(cs.getInfoAboutCustomer());
            }

And that's the content of SqliteDataAccess.cs:

     public List<VhsMovie> GetAllMoviesFromDb()
        {
            using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
            {
                var output = cnn.Query<VhsMovie>("select  movieTitle, ID, renterID from Movies", new DynamicParameters());
                return output.ToList();
            }
        }


private  string LoadConnectionString(string id = "Default"){

                return ConfigurationManager.ConnectionStrings[id].ConnectionString;
            }

I do not know why DataLayer sees Db but Test does not. Could you please help me :)

S.Rucinski
  • 109
  • 1
  • 8
  • 1
    Add reference to DataLayer project in your Tests project. Use debugger as well to see what is null when executing the test1() – Jawad Dec 06 '19 at 16:38
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jaskier Dec 06 '19 at 16:38
  • Do check your connection string. It seems like you are loading the Data Source=.\Customers.db; when customers.db is not in Tests. update that to include full path. – Jawad Dec 06 '19 at 16:39
  • 1
    I can tell you why it will occur the error. Configuration only read current project app.config. When you are running the second project, its path should be the second project path. Therefore, I suggest that you don't access data from other project app.config. – Jack J Jun Dec 09 '19 at 06:30

1 Answers1

1

You are trying to load the connection string from the App.config, but the Tests project will be looking for that connection string within it's own project's App.config. So just create an App.config for your Tests project and specify the connection string there too. Make sure it points to the right location (I would suggest a seperate database for testing).

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64