views:

52

answers:

2

I am trying to use MSTest on our code base. Now I am running into different problems during this.

We implicitly use _getDefaultName from Microsoft.Practices.EnterpriseLibrary.Data and it gets the default connection string in the current Assembly's App.config. Since this test project would be a new assembly/project it is not able to find the connection string in the source project.

I can avoid this by hard coding the connection string or giving the path of the App.config explicitly in source code.

But I do not want to change the source code for the sake of test code, so is there a way to specify or change the current running assembly?

Would my life be simple if I use any other testing framework?

A: 

Well, your life might be simpler if you used other testing frameworks ;) (I use and like NUnit) but that's unlikely to resolve this issue.

I'd suggest just copying the App.config data into the unit test assembly. You may want to change it, as presumably you don't want your tests running against a production database.

If you want to do unit testing (as opposed to integration testing), consider writing tests that don't access the database at all. This is generally accomplished through the use of a layered architecture, dependency injection, and isolation (mock object) frameworks. There's a lot on these individual topics on SO; please post a comment if you're interested but are having trouble finding resources.

TrueWill
A: 

You might consider an approach as outlined in another post. I took one of the examples in that link, and changed it to look like your case:

    public static class Context
    {
        private static string ConnectString;

        public static string Connect
        {
            get { return ConnectString ?? DataFactory.instance; }
            set { ConnectString= value; }
        }

        private class DataFactory
        {
            static DataFactory() { }
            internal static readonly string instance
                   = EntLib.Data._getDefaultName;
        }
    } 

Now when production code runs, it gets its configuration from EntLib - from the App.Config file. But when you run your mstest code, you make sure to inject the connection string you need into Context.Connect before any part of the assembly is tested.

Brent Arias