-1

I'm trying to set a Test environment for my actual project but having troubles connecting to my Test db, we don't have a mock.

I use some code to determine if is a Design Time or Run Time like this:

        ConnectionStringSettings connString = null;
        string myConnString = null;
        bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
        if (designMode)
        {
            connString = new ConnectionStringSettings()
            {
                ConnectionString = "metadata=res://*/Seguridad.csdl|res://*/Seguridad.ssdl|res://*/Seguridad.msl;provider=System.Data.SqlClient;provider connection string=\"data source=****ServerName****;initial catalog=****DbName****;persist security info=True;user id=****user****;password=****pwd****;MultipleActiveResultSets=True;App=EntityFramework\""
            };
            DebugConnection Cx = DebugModeConnections.GetConnection("Desarrollo");
            _usuario = Cx.Usuario;
            _password = Cx.Password;
            _ServerName = Cx.Server;
            _DbName = Cx.Db;
        }
        else
        {
            foreach (ConnectionStringSettings conn in ConfigurationManager.ConnectionStrings)
            {
                if (conn.Name == ConfiguracionApp.NombreContextoDb)
                    connString = conn;
            }
        }

It works fine for my Design and run time and allow me to use real data for GUI design. But now I want to implement test and it doesn't work as I expected.

There is a way to determine if the compiler is on Test Time or something like LicenseManager.UsageMode wich I can use ?

Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101

1 Answers1

2

Since you told in comments that you use VS Testing framework, you should have a testing project. If your files are linked in the testing project (not as a dll or another project) you could define a custom symbol under Conditional compilation symbols in project properties, like for example TESTING then do a simple

#if TESTING
// this code is compiled only in tests
#endif

NOTE: you must do it for all configurations (Debug|Release) and platforms (x86,AnyCpu).

Build Project Properties

ExtremeMultiman
  • 112
  • 1
  • 4