0

I am pretty new in C# and .NET (I came from Java EE ecosystem) and I am finding some difficulties working on a SharePoint 2013 project trying to add unit test (unsing NUnit) that have to test the behavior of a DAO project defined into my solution.

I try to explain you my situation in details. Into my solution I have several projects. One of these projects (named somethinbg like MyProjectSqlEngine) contain the DAOs classes performing the queries on my dtabase. Another project is a NUnit test project where I am trying to define some tests to test the DAO methods.

So, into my DAO project I have this class named IndirizziProtocolliSQL:

namespace MyProject.Protocollo.QueryUtils
{
    public class IndirizziProtocolliSQL
    {
        /**
         * <summary>
         * Ritorna la lista dei siti
         * </summary>
         */
        public static List<string> GetListaIndirizziSiti(DBConnection dbConf)
        {
            string url = null;

            List<string> urlList = new List<string>();

            string query = PROT_INDIRIZZI_PROTOCOLLI.SELECT_LISTA_INDIRIZZI_SITI;
            using (SqlConnection con = ArxeiaConnection.getARXEIAStringConnection(dbConf.Tenant + "_PROTOCOLLO"))
            {
                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    try
                    {
                        con.Open();

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    url = reader.GetString(reader.GetOrdinal("Url"));
                                    urlList.Add(url);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }
            return urlList;
        }

    }
}

It contains the method named GetListaIndirizziSiti(DBConnection dbConf) that take a DBConnection object and use it to obtain the connection (the SqlConnection object) and then perform a SQL query. My query is defined as a static string properties into the PROT_INDIRIZZI_PROTOCOLLI class (but this is not important):

string query = PROT_INDIRIZZI_PROTOCOLLI.SELECT_LISTA_INDIRIZZI_SITI;

Ok, I can call the previous GetListaIndirizziSiti(DBConnection dbConf) method from the code of a main project that have the MyProjectSqlEngine as reference. In a class of this main project I have something like this:

public override void Execute(Guid targetInstanceId)
{

    DBConnection dbConfig = new DBConnection();
    dbConfig.Tenant = "ABC";

    List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
}

It works fine, the query performed by this GetListaIndirizziSiti() is correctly performed and I obtain the expected values into the urlList variable.

The problem is that I want to do integration tests testing all my DAO methods using NUnit (in this way I can develop and test all my DAO methods before implement the service application logic).

So I created a new NUnit test project, into my solution containing this class with a minimalistic test method to test the previos DAO method:

namespace Tests
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void GetIndirizzoProtocollo_XXX_YYY_ReturnSingleValue()
        {
            DBConnection dbConfig = new DBConnection();
            dbConfig.Tenant = "ABC";

            List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);
            //string test = IndirizziProtocolliSQL.Test();

            Assert.Pass("OK");
        }

    }
}

The problem is that performing this test method when arrive on this line (I see it debugging it):

List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

it is thrown the following exception:

Exception thrown: 'System.IO.FileNotFoundException' in TestProject.dll
An exception of type 'System.IO.FileNotFoundException' occurred in TestProject.dll but was not handled in user code
Could not load file or assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Why? What am I missing? What exactly means that the System.Data.SqlClient assembly can't be load. How can I try to fix this issue?

NOTE: I have put the main project as reference of this NUnit test project so in the test code. This main project has as my DAO project as dependency. I don't know if this dependencies chain can create some problem to NUnit.

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • [Possible duplicate](https://stackoverflow.com/questions/43521867/system-io-filenotfoundexception-with-sqlclient)? – Chris Dec 28 '18 at 14:57
  • How are you referencing the project from within the test project? As you've come from a Java background, I'm wondering if you have referenced the `.dll` file rather than adding a project reference? – Fenton Dec 28 '18 at 14:58
  • @Fenton I added my main project (the one that have all the other projects, including the MyProjectSqlEngine, as reference) as project reference of my test project – AndreaNobili Dec 28 '18 at 15:02
  • When you build, do you see the `System.Data.SqlClient.dll` in your test project's bin folder? – Fenton Dec 28 '18 at 15:08
  • @Fenton no this dll is not into the build path of my test project. What can I do? – AndreaNobili Dec 28 '18 at 15:13

0 Answers0